从用户输入更改 setState
change setState from user Input
我是 React 的新手,正在尝试使用 React。我正在制作一组立方体,当用户单击时,它们会在颜色之间交替。
为了了解有关 setState 的更多信息,我添加了一个输入,这样当用户输入一种颜色时,立方体的颜色就会发生变化。问题是立方体的颜色发生了变化,但是当我单击立方体时,它又恢复为默认颜色。我想让立方体改变用户输入的颜色。
我尝试了什么?
我尝试将 this.setState({color: 'pink'})
中的粉红色更改为 setColor(),但它不起作用。我环顾四周,但找不到任何可以回答我问题的东西。
我创建了问题 here。
class TicTacToe extends Component {
state = {
color: 'black',
colors: 'white',
count: 0
}
changeColor(c){
this.setState({ count: this.state.count + 1 })
if(this.state.count % 2 === 0){
this.setState({color: 'pink'})
this.setState({colors: 'grey'})
} else if (this.state.count % 2 !== 0){
this.setState({color: 'grey'})
this.setState({colors: 'pink'})
}
}
setColor(color){
return this.setState({color: color})
}
setColors(color){
this.setState({colors: color})
}
render(){
return (
<div className="main">
<div className="inputFields">
<span> Color One:
<input value={this.state.color}
onChange={(e)=> this.setColor(e.target.value)} /> </span>
<br /><br />
<span> Color Two:
<input value={this.state.colors}
onChange={(e)=> this.setColors(e.target.value)} /> </span>
</div>
<div onClick= {(c)=> this.changeColor()}>
<div className='square' style={{backgroundColor: this.state.color}}></div>
<div className='square' style={{backgroundColor: this.state.colors}}></div>
<div className='square' style={{backgroundColor: this.state.color}}></div>
<br />
<div className='square' style={{backgroundColor: this.state.colors}}></div>
<div className='square' style={{backgroundColor: this.state.color}}></div>
<div className='square' style={{backgroundColor: this.state.colors}}></div>
<br />
<div className='square' style={{backgroundColor: this.state.color}}></div>
<div className='square' style={{backgroundColor: this.state.colors}}></div>
<div className='square' style={{backgroundColor: this.state.color}}></div>
</div>
<span> This is the count: {this.state.count} </span>
</div>
)
}
}
export default TicTacToe;
它会回到默认值 onClick
,因为 onClick
您正在手动设置 colors
,而且您不需要计数变量来在 [=13= 之间切换].计数状态也没有正确使用。您所需要的只是使用函数式 setState 并实现 changeColor
like
changeColor(c){
this.setState(prevState => ({
color: prevState.colors,
colors: prevState.color
}))
}
勾选
勾选
我是 React 的新手,正在尝试使用 React。我正在制作一组立方体,当用户单击时,它们会在颜色之间交替。
为了了解有关 setState 的更多信息,我添加了一个输入,这样当用户输入一种颜色时,立方体的颜色就会发生变化。问题是立方体的颜色发生了变化,但是当我单击立方体时,它又恢复为默认颜色。我想让立方体改变用户输入的颜色。
我尝试了什么?
我尝试将 this.setState({color: 'pink'})
中的粉红色更改为 setColor(),但它不起作用。我环顾四周,但找不到任何可以回答我问题的东西。
我创建了问题 here。
class TicTacToe extends Component {
state = {
color: 'black',
colors: 'white',
count: 0
}
changeColor(c){
this.setState({ count: this.state.count + 1 })
if(this.state.count % 2 === 0){
this.setState({color: 'pink'})
this.setState({colors: 'grey'})
} else if (this.state.count % 2 !== 0){
this.setState({color: 'grey'})
this.setState({colors: 'pink'})
}
}
setColor(color){
return this.setState({color: color})
}
setColors(color){
this.setState({colors: color})
}
render(){
return (
<div className="main">
<div className="inputFields">
<span> Color One:
<input value={this.state.color}
onChange={(e)=> this.setColor(e.target.value)} /> </span>
<br /><br />
<span> Color Two:
<input value={this.state.colors}
onChange={(e)=> this.setColors(e.target.value)} /> </span>
</div>
<div onClick= {(c)=> this.changeColor()}>
<div className='square' style={{backgroundColor: this.state.color}}></div>
<div className='square' style={{backgroundColor: this.state.colors}}></div>
<div className='square' style={{backgroundColor: this.state.color}}></div>
<br />
<div className='square' style={{backgroundColor: this.state.colors}}></div>
<div className='square' style={{backgroundColor: this.state.color}}></div>
<div className='square' style={{backgroundColor: this.state.colors}}></div>
<br />
<div className='square' style={{backgroundColor: this.state.color}}></div>
<div className='square' style={{backgroundColor: this.state.colors}}></div>
<div className='square' style={{backgroundColor: this.state.color}}></div>
</div>
<span> This is the count: {this.state.count} </span>
</div>
)
}
}
export default TicTacToe;
它会回到默认值 onClick
,因为 onClick
您正在手动设置 colors
,而且您不需要计数变量来在 [=13= 之间切换].计数状态也没有正确使用。您所需要的只是使用函数式 setState 并实现 changeColor
like
changeColor(c){
this.setState(prevState => ({
color: prevState.colors,
colors: prevState.color
}))
}
勾选
勾选