javascript / beginner
Snippet
The useState Hook for State Management
The useState hook allows functional components to maintain their own internal state. It returns an array with the current value and a function to update it.
snippet.js
1
2
3
4
5
6
import { useState } from 'react';function Counter() {const [count, setCount] = useState(0);return <p>Count: {count}</p>;}
react
Breakdown
1
import { useState } from 'react';
Imports the state hook from the React library.
2
const [count, setCount] = useState(0);
Initializes count at 0 and provides setCount to change it.