abidishajia
abidishajia

Reputation: 252

change setState from user Input

I'm fairly new to React and experimenting with React. I am making a set of cubes that when a user clicks, alternate between colors.

To learn more about setState, I added an input so that when the user enters a color, the color of the cubes changes. The problem is the color of the cube changes, but when I click on the cube it goes back to the default one. I want the cubes to change the colors that are inputted by the user.

What have I tried? I tried changing pink in this.setState({color: 'pink'}) to setColor(), but it doesn't work. I looked around here, but couldn't find anything that would answer my question.

I've created the issue 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;

Upvotes: 0

Views: 503

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 282120

It goes back to the default onClick, because onClick you are manually setting the colors, Also you don't need a count variable to alternate between colors. The count state is also not used properly. All you need is to use functional setState and implement changeColor like

changeColor(c){
    this.setState(prevState => ({
      color: prevState.colors,
      colors: prevState.color
    }))
  }

Working Demo

Check When to use functional setState

Check setState doesn't update the state immediately

Upvotes: 2

Related Questions