what will happen if usestate does not initialised with any value?
Eg: const [growth, setGrowth] = useState();
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.
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.
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 .
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.
The implicit value is then (as it is for any elided function parameter) undefined
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With