Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when we use usestate without any parameters (in React hooks)?

what will happen if usestate does not initialised with any value?

Eg: const [growth, setGrowth] = useState();

like image 1000
Abhinav Kant Avatar asked Feb 14 '20 06:02

Abhinav Kant


People also ask

Why we use useState in React Hooks?

The React useState Hook allows us to track state in a function component. State generally refers to data or properties that need to be tracking in an application.

What is the use of useState () in Hook?

useState is a Hook that allows you to have state variables in functional components. You pass the initial state to this function and it returns a variable with the current state value (not necessarily the initial state) and another function to update this value.

Can we use useState without setState?

Yes, but you might not want to. With class components we would keep all of our state in one object. Then when we changed state with setState , we would provide an object and the API would "merge" its contents into the existing state for us. This doesn't happen with setting state in function components with useState .

Why we use useState instead of variable?

ANSWER: The reason is if you useState it rerenders the view. Variables by themselves only change bits in memory and the state of your app can get out of sync with the view. In both cases a changes on click but only when you use useState the view correctly shows a 's current value.


1 Answers

The implicit value is then (as it is for any elided function parameter) undefined.

like image 119
AKX Avatar answered Oct 25 '22 20:10

AKX