react-native - 从样式表响应 native 访问状态

标签 react-native

我正在尝试根据状态设置容器的背景,但它一直向我显示错误 bg 未定义

constructor(props) {
    super(props)
    this.state = {
        bg:'#fff'
    }
}

render(){
    return(<View style={styles.container} />)
} 

const styles = StyleSheet.create({
    container:{
        flex: 1,
        backgroundColor: this.state.bg,
    }
})

最佳答案

更改此代码:

return <View style={[styles.container, backgroundColor: this.state.bg]}/>

对于此代码:
return <View style={[styles.container, {backgroundColor: this.state.bg}]}/>

关于react-native - 从样式表响应 native 访问状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54583037/

相关文章:

javascript - 如何将多个项目推送到一个数组中并加载到 native react 中的异步存储中?

android - 从 React-Native 代码调用 Android Activity

javascript - react native 刷新组件

react-native - 如何更改 Expo React native 默认端口 19000

react-native - 在 Android 中隐藏 FlatList (React Native) 中的滚动条

javascript - React-Native:使用 panResponder.panHandlers (nativeElement) 获取可拖动对象的 X 和 Y 坐标

reactjs - 使用 react 导航将导航历史记录重置为登录屏幕

javascript - React native 模块允许您复制/选择 View 中包含的文本

javascript - 如何从项目列表中选择 ScrollView 中的中心项目

react-native - 在 React Native 中动画旋转到最近的值