更优雅的状态设置方式
more elegant way to set state
下面的函数中setState有没有更优雅或者最佳实践的方式?
startStop(){
if(this.state.start === 'Start') startStop = 'Stop'
else startStop = 'Start'
this.setState({start:startStop})
}
this.setState({开始:this.state.start==='Start' ? 'Stop' : 'Start'})
如果你有类似的东西:
state = {
keepGoing: false,
}
然后您可以执行以下操作来切换:
this.setState({ keepGoing: !this.state.keepGoing });
对于标志,而不是字符串 Start/Stop 使用 true-false,
例如:-
constructor(props){
this.state={
start:false
}
}
startStop(){
this.setState({start:!this.state.start})
}
render(){
return<Text>{this.state.start?'Running':'Stopped'}</Text>
}
下面的函数中setState有没有更优雅或者最佳实践的方式?
startStop(){
if(this.state.start === 'Start') startStop = 'Stop'
else startStop = 'Start'
this.setState({start:startStop})
}
this.setState({开始:this.state.start==='Start' ? 'Stop' : 'Start'})
如果你有类似的东西:
state = {
keepGoing: false,
}
然后您可以执行以下操作来切换:
this.setState({ keepGoing: !this.state.keepGoing });
对于标志,而不是字符串 Start/Stop 使用 true-false,
例如:-
constructor(props){
this.state={
start:false
}
}
startStop(){
this.setState({start:!this.state.start})
}
render(){
return<Text>{this.state.start?'Running':'Stopped'}</Text>
}