Tuesday, October 25, 2022

13/365 Complex State Data

 Today in the Meta course I learned more about state and the useState hook. In particular, this segment discussed how to handle complex data, like objects, in the state. With primitive data the provided setter method given by the useState function allows you to simply change the state. However, when your state contains an object, it is costly and inefficient to update the entire object. Instead, the best practice is to use the spread operator ( three dots like ...) to update only the needed value in the object. This is done by creating a copy of the object, changing the values as needed in the copy, and then setting the state object to your new object. This avoids many errors and unexpected behaviors that arise from updating your state object directly. 

No comments:

Post a Comment

190: Sablier

 The CodeHawks platform has an upcoming audit on the Sablier protocol, so I decided to read through the docs and familiarize myself with the...