React write a simple counter (only need an explication)

Tell us what’s happening:
Hi guys,
Quick question why in this case we need to write count: state.count+1 and not just count: count+1 ?

Your code so far

// change code below this line
increment() {
  this.setState(state => ({
    count: state.count + 1

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0.

Challenge: Write a Simple Counter

Link to the challenge:

Because count is not a variable. state is a variable (parameter in this case) available to the function.

That being said, you could destructure state to obtain count and use it as you wanted to do.

1 Like