首页 > 解决方案 > 我想在有条件的本机反应中返回 2 个按钮

问题描述

我正在使用 react native 0.59.9,我想在 1 个条件下返回 2 个按钮。但它没有用。如果只有一个按钮,它工作正常,但如果我放 2 个按钮,我得到了一些错误

我试过把()有条件的,但它没有用

{this.props.options.config.editable ?
    <Button
        onPress={ () => this.bottomSheet.open()
        }
        color={buttonTextColor}
        title={locals.config.title}
    />
   //this button make the error
    <Button 
        onPress={() => this.bottomSheet.open()
        }
        color={buttonTextColor}
        title={locals.config.title}
    />
    :
    <View/>
}

我想要有条件的返回那个 2 按钮

标签: reactjsreact-nativereact-native-android

解决方案


将两个按钮包装在同一个容器或 React.fragment 中。

{ this.props.options.config.editable ?
    <React.Fragment>
        <Button
            onPress={
                Platform.OS === 'ios'
                    ? this._onPressImage
                    : () => this.bottomSheet.open()
            }
            color={buttonTextColor}
            title={locals.config.title}
        />
        //this button make the error
        <Button 
            onPress={
                Platform.OS === 'ios'
                    ? this._onPressImage
                    : () => this.bottomSheet.open()
            }
            color={buttonTextColor}
            title={locals.config.title}
        />
    </React.Fragment>
:
<View/>

}


推荐阅读