首页 > 解决方案 > 使用按钮更改背景颜色 react-native

问题描述

我正在尝试通过单击按钮来更改我的视图背景颜色。但是什么也没发生(没有错误)。

export default class App extends React.Component{

    state = {
        color: "#fff"
        }

        render() {
            
            return(
                
                <View style={{backgroundColor: this.state.color, flex: 1, alignItems:'center', justifyContent:'center',}}>
                  
                  <Button  onClick={() => { this.setState({color: "#00ff00" }) }} title="selam"></Button>

                </View>
                      
            );
        }
};

标签: react-native

解决方案


You have to use onPress not onClick

<Button onPress={() => { this.setState({color: "#00ff00" }) }} title="selam"></Button>

推荐阅读