首页 > 解决方案 > React Native,在 Render 中调用另一个函数

问题描述

我有这个 SMN() 函数,并在其中创建了站点函数作为常量。所以我需要在 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>
    )
}

标签: javascripthtmlfunctionreact-native

解决方案


将您的站点作为一个组件,就像这样:

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>
    )
}

推荐阅读