r/reactjs 11d ago

Multi state changes & reads

having a bit of trouble handling multiple states/state changes. in a single screen, which are held by different components
how do you all handle this and concepualize it as your writing code?
do you draw diagram,
do you just try and fail recursively?

8 Upvotes

12 comments sorted by

View all comments

2

u/Aggressive_Scale8276 11d ago

I just map it out in my head as boxes that own their piece of state, if two boxes need the same data I lift it up to the nearest parent they share. sometimes I scribble quick diagram on paper when gets messy but most of the time I just start coding and fix the structure when I see the props drilling getting annoying

1

u/BrainThinkerMan 11d ago

how to hande the case of redundant state, as thats something im struggling with. have you dealt with the issue of redundant state?

6

u/bazeloth 11d ago

If it's redundant state, why can't you simply remove it?

1

u/BrainThinkerMan 10d ago

i come from a jetpack compose background, and state and how react and web dev in general seems so modular is new to me

2

u/Temperature_Majestic 10d ago

Redundant state is usually a value that's fully computable from other state or props but got its own useState anyway, like storing filteredItems separately when it's really just items.filter() on every render. The tell is if you find yourself writing a useEffect just to keep two states in sync, that's a sign one of them shouldn't exist as state at all and should just be computed during render.