score:3

Accepted answer
togglepersonhandler =()=>{
    const doesshow = this.state.showperson;
    this.setstate({showperson : !doesshow});
}

your code can be written as the following. we are setting the new state, using the setstate call, using the values in the previousstate. we get the previous value of showperson and 'reverse' the truth value using the negation ! operator.

togglepersonhandler = () => {
    this.setstate((previousstate) => {
         return {
             showperson : !previousstate.showperson
         }
    });
}

this is the recommended format to write setstate calls that depend on the previous state.

truth table

this is how the negation operator affects a boolean value, shown in a truth table

----------------------------
| showperson | !showperson |
----------------------------
|    true    |     false   |
----------------------------
|    false   |     true    |
----------------------------

Related Query

More Query from same tag