Why do we use array destructuring (square brackets notation) in useState?

Posted by Jessica Taylor | Updated on

Why do we use array destructuring (square brackets notation) in useState?

When we declare a state variable with useState, it returns a pair — an array with two items. The first item is the current value, and the second is a function that updates the value. Using [0] and [1] to access them is a bit confusing because they have a specific meaning. This is why we use array destructuring instead.

For example, the array index access would look as follows:

Whereas with array destructuring the variables can be accessed as follows:


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers