0% found this document useful (0 votes)
34 views5 pages

36 - Usestate Hook With Object

useState Hook allows keeping track of state in functional components. It can manage strings, numbers, booleans, arrays, and objects. When updating object state, the entire state gets overwritten. To update a single property, the JavaScript spread operator (...) can be used to copy the previous state and only overwrite the desired property value being updated.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views5 pages

36 - Usestate Hook With Object

useState Hook allows keeping track of state in functional components. It can manage strings, numbers, booleans, arrays, and objects. When updating object state, the entire state gets overwritten. To update a single property, the JavaScript spread operator (...) can be used to copy the previous state and only overwrite the desired property value being updated.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PART-36

useState Hook
With Object
PRESENTER: MOHAMMAD ADIL
REACT
useState Hook REACT

• Hook uses useState() functional component for setting and retrieving


state.
• The useState Hook can be used to keep track of strings, numbers,
booleans, arrays, objects, and any combination of these!
Updating Objects in State REACT

• When state is updated, the entire state gets overwritten.


• What if we only want to update the name of a person ?
• If we only called setPerson({name: “Zain"}), this would remove the
age, height, and isYoung from our state.
• We can use the JavaScript spread operator (…) to help us.
Spread Operator REACT

• The JavaScript spread operator (...) allows us to quickly copy all or


part of an existing array or object into another array or object.
Updating Objects in State REACT

• Because we need the current value of state, we pass a function into our
setPerson function. This function receives the previous value.
• We then return an object, spreading the previousState and overwriting
only the name.

You might also like