𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴 𝗥𝗲𝗮𝗰𝘁 𝘂𝘀𝗲𝗦𝘁𝗮𝘁𝗲
Definitions often confuse people. You learn best by building small projects.
useState stores data. It updates your screen when data changes.
The code looks like this: const [state, setState] = useState(initialValue);
- state: current value
- setState: function to update the value
- initialValue: starting value
Example 1: Password Toggle. You start with a false value. The input hides the password. You click a button. The state changes to true. The input shows the password.
Example 2: Todo App. This project helps you manage lists.
- Add tasks
- Edit tasks
- Delete tasks
Every change updates the state array. React updates your list automatically.
Follow this flow:
- State changes
- Component re-renders
- UI updates
Use state when data changes and your UI must update.
Source: https://dev.to/jayashree_a84b6eff7bc414e/learning-react-usestate-through-practical-examples-195o