-4

Can someone explain me what is going on here?

const [counter, setCounter] = useState(0);

Is it some kind of assignment?

ddream
  • 3
  • 1
  • The React docs might be able to help you out here. What part in particular is confusing? Obviously it's an assignment, that's what `=` does. Are you asking about the destructuring? JS docs might be able to help you out here. – Dave Newton Jan 07 '21 at 16:03
  • 3
    [Destructuring assignment - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) – Andreas Jan 07 '21 at 16:04
  • 1
    useState(0) returns a tuple where the first parameter count is the current state of the counter and setCounter is the method that will allow us to update the counter's state. We can use the setCounter method to update the state of count anywhere - In this case we are using it inside of the setCount function where we can do more things; the idea with hooks is that we are able to keep our code more functional and avoid class based components if not desired/needed. ref. https://stackoverflow.com/questions/53165945/what-is-usestate-in-react – Hidayt Rahman Jan 07 '21 at 16:20

2 Answers2

1

I think you might need to take a look into the React Hooks docs

luissmg
  • 545
  • 4
  • 9
0

It's a destructuring assignment on the results of calling the useState function:

  • the first element of the array is the value
  • the second element is a function to change value.
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Prince Sodhi
  • 2,845
  • 2
  • 21
  • 35