Array Destructuring with useState
What you'll learn: How to use array destructuring syntax to cleanly declare and name your state variables when calling useState.
The Convention You'll See Everywhere
You've seen that useState returns a two-element array: [currentValue, setterFunction]. Instead of accessing these by index (result[0] and result[1]), React developers universally use array destructuring to unpack them into named variables in one line.
This is the standard pattern:
const [count, setCount] = useState(0);
This single line does three things:
- Calls
useState(0)which returns an array - Takes the first element (the current state value) and assigns it to
count - Takes the second element (the setter function) and assigns it to
setCount
Why This Syntax?
Array destructuring lets you choose your own names for both the state variable and its setter. This is different from object destructuring where names are predetermined. With arrays, position matters, not names:
// All of these work the same way:
const [count, setCount] = useState(0);
const [userAge, setUserAge] = useState(0);
const [temperature, setTemperature] = useState(0);
Each declaration creates uniquely named variables, even though useState always returns the same structure.
The Naming Convention
By convention, the setter is named set + capitalized state variable name:
count→setCountisOpen→setIsOpenuserName→setUserName
This convention makes code predictable and readable across all React projects.
Key Takeaway: Array destructuring with useState is the standard way to declare state in function components, allowing you to pick meaningful names for both your state variable and its setter function in one concise line.