Class-based components and functional components both have different ways of storing and setting values in them.
In class-based components, this.state is used which is usually of an object nature and holds all the values needed in the components, here setState() function is used to do a shallow update to this.state object. (It will merge the prev and new object)
In function-based components state can be stored using useState hook. The name of the state variable can be anything. Just like the class component here, we have two items returned by the useState hook. The first item represents the variable and the second represents the setter function.
Now,
To useState variable is declared like,
const [banana, setBanana] = useState("initial value - could be string, number, object");
To use the variable in the component just use the variable name "banana" and set a new value in the variable using the setter function like,
setBanana("good banana");
This will change the initial value to whatever you set it to. If the variable was an object, it will be replaced by the new object not shallow merged.
Also, inside a function-based component, you can have any number of useState variables. The important thing to keep in mind is if multiple of them are called in a function call batching is performed.
Refs:-
- https://react.dev/reference/react/Component#setstate
- https://legacy.reactjs.org/docs/hooks-state.html#declaring-a-state-variable