【问题标题】:React Native, Calling a Function from Another one in RenderReact Native,在 Render 中调用另一个函数
【发布时间】:2020-12-14 23:00:05
【问题描述】:

我有这个 SMN() 函数,并在其中创建了 Site 函数作为 Const。所以我需要在 Render() 函数中调用 Site 函数。这是主要功能代码:

SMN() {
    const Site = () => {
            return (
                <View style={{ height: 400 }}>
                    <WebView source={{ uri: 'https://www.google.com' }} style={{ marginTop: 20 }} />
                </View>
        );
     }
});

这是 Render() 函数,我想从中调用 Site 函数,我使用过: this.SMN().Site ,这不会抛出错误,但不会显示任何错误。

render() {
    return (
         </View>
            <View>{this.SMN().Site}</View>
        </View>
    )
}

【问题讨论】:

  • 为什么另一个函数里面有一个函数?

标签: javascript html function react-native


【解决方案1】:

像这样将您的网站作为一个组件:

const Site = () => {
        return (
            <View style={{ height: 400 }}>
                <WebView 
                    source={{ uri: 'https://www.google.com' }} 
                    style={{ marginTop: 20 }} />
            </View>
    );
 }

并且在你的渲染函数中像这样使用它:

render() {
    return (
         </View>
            <View><Site /></View>
        </View>
    )
}

【讨论】:

  • @Chaliem 我收到了这个错误:ReferenceError: Can't find variable: Site
  • 您忘记导入网站了吗?
【解决方案2】:

我认为你想从 SMN() 获得你想要的回报。所以这就是我认为你想要的解决方案。

SMN() {
   
            return (
                {
                 Site : () => (
                                  <View style={{ height: 400 }}>
                                    <WebView source={{ uri: 'https://www.google.com' }} 
                                             style={{marginTop:20 }} />
                                   </View>
                               )
                 }
               );
             
});


render() {
    return (
         </View>
            <View>{this.SMN().Site}</View>
        </View>
    )
}

【讨论】:

  • @Changboon Lee,我收到了这个错误:TypeError: this.SMN is not a function. (In 'this.SMN()', 'this.SMN' is undefined)
  • @HowardJohn 请告诉我该代码正在处理的所有代码。
猜你喜欢
  • 2021-11-20
  • 1970-01-01
  • 2021-03-17
  • 1970-01-01
  • 2021-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多