React native setState未更新react native中的样式
首先,我对react native非常陌生,我自己也在努力学习它,并在它周围玩耍 我有一个变量名mbackgroundColor。我用默认值在构造函数中初始化它React native setState未更新react native中的样式,react-native,state,React Native,State,首先,我对react native非常陌生,我自己也在努力学习它,并在它周围玩耍 我有一个变量名mbackgroundColor。我用默认值在构造函数中初始化它 constructor(props) { super(props) this.state = { mbackgroundColor: 'green' };} 我在JSX中有一个按钮,当我点击它时,我成功地显示了一个警报 <View style={ [{ height: 50, }, {backgroundColor :
constructor(props) {
super(props)
this.state = {
mbackgroundColor: 'green'
};}
我在JSX中有一个按钮,当我点击它时,我成功地显示了一个警报
<View style={
[{
height: 50,
}, {backgroundColor : this.state.mbackgroundColor}]
}>
<Button onPress={this.onPressLearnMore}
title="Learn More"
color="#841584"
accessibilityLabel="Learn more about this purple button"
/>
我已附上代码
谢谢你的帮助:) 更改您的代码
来自
onPressLearnMore() {
Alert.alert('on Press!');
this.setState = {
mbackgroundColor: 'white'
};}
至
onPressLearnMore() {
this.setState({mbackgroundColor: 'white'},()=>Alert.alert('on Press!'))
}
更改代码
来自
onPressLearnMore() {
Alert.alert('on Press!');
this.setState = {
mbackgroundColor: 'white'
};}
至
onPressLearnMore() {
this.setState({mbackgroundColor: 'white'},()=>Alert.alert('on Press!'))
}
只需设置如下状态:
this.setState({mbackgroundColor: 'white'})
而不是使用:
this.setState = {
mbackgroundColor: 'white'
}
只需设置如下状态:
this.setState({mbackgroundColor: 'white'})
而不是使用:
this.setState = {
mbackgroundColor: 'white'
}
此
在onpress learnmore
中不引用当前类,该类将抛出错误,要获得此工作,请将代码更改为以下内容
onPressLearnMore = () => {
Alert.alert('on Press!');
this.setState({
mbackgroundColor: 'white',
});
}
要更改值is state,我们使用此格式this.setState({})
而不是this.state={}
你甚至不需要构造函数
改变
constructor(props) {
super(props)
this.state = {
mbackgroundColor: 'green'
};
}
至
export default class App extends Component<Props> {
state = {
mbackgroundColor: 'green',
};
onPressLearnMore = () => {
Alert.alert('on Press!');
this.setState({
mbackgroundColor: 'white',
});
};
....
导出默认类应用程序扩展组件{
状态={
mbackgroundColor:“绿色”,
};
onPressLearnMore=()=>{
警惕。警惕('on Press!');
这是我的国家({
mbackgroundColor:“白色”,
});
};
....
此
在onPressLearnMore中不引用当前类,这将引发错误,要获得此工作,请将代码更改为以下内容
onPressLearnMore = () => {
Alert.alert('on Press!');
this.setState({
mbackgroundColor: 'white',
});
}
要更改值is state,我们使用此格式this.setState({})
而不是this.state={}
你甚至不需要构造函数
改变
constructor(props) {
super(props)
this.state = {
mbackgroundColor: 'green'
};
}
至
export default class App extends Component<Props> {
state = {
mbackgroundColor: 'green',
};
onPressLearnMore = () => {
Alert.alert('on Press!');
this.setState({
mbackgroundColor: 'white',
});
};
....
导出默认类应用程序扩展组件{
状态={
mbackgroundColor:“绿色”,
};
onPressLearnMore=()=>{
警惕。警惕('on Press!');
这是我的国家({
mbackgroundColor:“白色”,
});
};
....
setState是一个函数。您正试图分配给它。您需要传递对象并调用该函数。您应该将上下文绑定到您的onPressLearnMore
函数:@如果您面临此上下文问题,请始终使用箭头函数来解决此上下文问题。setState是一个函数。您正在尝试分配o它。你需要传递对象并调用函数。你应该将上下文绑定到你的onPressLearnMore
函数:@alok你面临这个上下文问题。总是使用箭头函数来解决这个上下文问题。谢谢!我必须做两个更改:onPressLearnMore(){this.setState({mbackgroundColor:'white'),()=>Alert.Alert('on Press!')}和{this.onPressLearnMore.bind(this)}这不起作用,因为这个
没有绑定到函数。@Alok代替绑定,你可以使用onPress={()=>this.onPressLearnMore}谢谢!我必须做两个更改:onPressLearnMore(){this.setState({mbackgroundColor:'white'),()=>Alert.Alert('on Press!')}){this.onPressLearnMore.bind(this)}这不起作用,因为这个
没有绑定到函数。@Alok代替绑定,你可以使用onPress={()=>this.onPressLearnMore}谢谢!我必须做两个更改:onPressLearnMore(){this.setState({mbackgroundColor:'white'},()=>Alert.Alert('on Press!'))和{this.onPressLearnMore.bind(this)}谢谢!我必须做两个改变:onPressLearnMore(){this.setState({mbackgroundColor:'white'},()=>Alert.Alert('on Press!'))}和{this.onPressLearnMore.bind(this)}显然你是一个优秀的开发人员,但更多的是一名教师。谢谢。显然你是一个优秀的开发人员,但更多的是一名教师。谢谢。