react-native - 使用按钮更改背景颜色 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>
);
}
};
解决方案
You have to use onPress not onClick
<Button onPress={() => { this.setState({color: "#00ff00" }) }} title="selam"></Button>
推荐阅读
- javascript - 函数实例上的“参数”和“调用者”属性
- python - mkdocs 和 PIP pip-20.0.2 的问题
- google-apps-script - 如何在 Google 电子表格中将范围转换为字符串?
- c++ - 向上转换 std::shared_ptr
到带有模板的 std::shared_ptr - java - 使用 Service Activator 方法的 Mockito bean
- javascript - 为什么使用钩子时会出现此错误“只能在函数组件的主体内部调用钩子。”?
- firebase - 我可以调用 Firebase Cloud Messaging 上的方法来发送新令牌吗?
- python - python:wget模块下载文件,没有任何扩展名
- reactjs - 条件输入框渲染
- java - 我们可以排序以将特定字母保持在顶部并按字母顺序排列吗?