Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React setstate take previous values

I wrote the below react component to handle the difficulty level of the minesweeper clone which I am making using react.

I have used radio buttons as difficulty input and would like to set the state of the component based on the selection made.

My problem is that every time I change my selection, the state is updated with my previous selection and not the current selected value. For instance, during page load the selected difficulty is ”Easy”. When I change the difficulty to say “Hard” the state still show the initial setting i.e “Easy” (I did a console log of the state to verify this).

Please help.

import React, {Component} from 'react';

class Difficulty extends Component{

  state = {
      height: 8,
      width: 8,
      mines: 10,
  };

  setDifficulty(event){
    let selectedDifficulty = event.target.value;
    if (selectedDifficulty === "Easy") {
        this.setState({
            height: 8,
            width: 8,
            mines: 10,
        });
    }
    if (selectedDifficulty === "Medium") {
        this.setState({
            height: 12,
            width: 12,
            mines: 20,
        });
    }
    if (selectedDifficulty === "Hard") {
        this.setState({
            height: 16,
            width: 16,
            mines: 40,
        });
    }
    this.props.updateDifficulty(this.state);
  }

  render(){
    return(
      <div className="game-difficulty">
        <div className="difficulty" onChange={this.setDifficulty.bind(this)}>
          <input type="radio" value="Easy" name="gender" defaultChecked="true" /> Easy
          <input type="radio" value="Medium" name="gender" /> Medium
          <input type="radio" value="Hard" name="gender" /> Hard
        </div>
      </div>
    );
  }
}

export default Difficulty;
like image 608
Shiv Baral Avatar asked Nov 03 '18 23:11

Shiv Baral


People also ask

How do I change the previous state in React JS?

state , we use this. setState() . This is a function available to all React components that use state, and allows us to let React know that the component state has changed. This way the component knows it should re-render, because its state has changed and its UI will most likely also change.

Does setState overwrite?

So in our case, when we are updating first name, setState is overwriting the complete name object with the new object passed in the setState, which has either firstName or lastName.


1 Answers

setState is asynchronous, so this.state will still contain the previous values if you try to use it right after calling setState.

You could instead do your logic that depends on this.state in a function given as the second argument to setState which will be run when the state has been updated.

setDifficulty(event) {
  let selectedDifficulty = event.target.value;
  let values;

  if (selectedDifficulty === "Easy") {
    values = {
      height: 8,
      width: 8,
      mines: 10
    };
  }
  if (selectedDifficulty === "Medium") {
    values = {
      height: 12,
      width: 12,
      mines: 20
    };
  }
  if (selectedDifficulty === "Hard") {
    values = {
      height: 16,
      width: 16,
      mines: 40
    };
  }

  this.setState(values, () => {
    this.props.updateDifficulty(this.state);
  });
}
like image 178
Tholle Avatar answered Oct 19 '22 20:10

Tholle