The state is specific to the component they are in and is controlled only by the component.
How to define states ?
What you see in the code example below is an older usage defined in the class component.
State access defined in class components is provided with "this" keyword.
In class components, the state is defined in the object.
How to define useState ?
- Basically useState is defined like this
- You can define the useState hook start value. For example ;
const [counter, setCounter] = useState(0); const [user, setUser] = useState(); or const [user, setUser] = useState(''); const [loading, setLoading] = useState(false);
Multiple states can be defined in function components.
The state value is updated with setState and returns the last updated value.
When the button is clicked below, we increase the setCounter by +1 and show the current value on the screen with the counter.
Thanks for reading.