DEV Community

Discussion on: React with Typescript

 
joaolss profile image
João Lucas Silva

I'm also not a fan of having object as state, but your explanation for more rerenders does not apply, because if your state is an object but you are passing a variable inside this object which is a primitive then value equality applies for react to rerender or not, and if you are passing the entire object, the same would apply for creating the object inline with all the separate states inside it, it would be a new reference every render and would trigger rerender, so it only applies for nested objects, which no one should ever do because it is so much worse than a plain object inside one single state

Thread Thread
 
konstagap profile image
konstagap

Why do u think nested object is something that no one should ever do? I deal with forms and I have lots of nested objects that has arrays of objects and so on, I use one single object to keep all together in form, to update one field I just shallow copy old object, change field, pass new reference to setState and it works no problem, Is there another way how to do it?

Thread Thread
 
joaolss profile image
João Lucas Silva

useReducer would be much better to deal with nested objects inside a state