3
ayushjn
3y

Is it fine to manage React states on your own ( kinda lifting up the state way ) in a medium size application? I've built 70% of the application without a lot of trouble. The only problem is there's no way to pass states to the sibling components without lifting up the state and lifting up the states decreases the modularity of the code. A lot of functions (handlers) have to be re-written in different components.

Would I be better off using Redux, etc. Thoughts?

Comments
  • 3
    It’s not hard to use a global state with global events without using redux and all of it’s annoying bits and peaces. No need to pass things around, use HOC‘s.
  • 2
    You should learn context
  • 1
    Learn hooks and for the case of disconnected state, useContext.

    Its specifically designed to solve that problem of having a state high up without having to pass it through every component on the way.

    And no, its not wrong to manage state that way if it works.

    If you start having real problem you might look into some state management libraries but useContext and the other state related hooks can handle quite complex applications out of the box, which means less modules to keep updated.
  • 2
    Generally speaking its totally ok to lift state up and etc. I generally decide where a state will "live" and then kinda make the call from there.

    As others said context works to spread it around without a lot of excessive prop drilling from way u high down low, but prop drilling isn't a problem all by itself either.

    If you're doing fine I'd avoid jumping to redux at this point. Redux is it's own kinda bear and if you're in good shape I'd be hesitant to reach for something like that.
  • 2
    @N00bPancakes I agree that you should avoid redux if you do not need it.

    Generally speaking, all such state frameworks adds overhead and if your unsure if you need them you probably don’t.

    And as you say, prop drill down is perfectly fine in many cases, especially between parts within a component since its less complex, a bit more code but also easier to follow.

    Context is most useful when you need to either share state between otherwise unrelated components in different parts of the tree, or reusing a component within multiple other components that by them self have nothing to do with this shared state.

    Context like most state management introduces a discontinuation of state trail making it more difficult for others to follow how information travels.

    Use it when you know its the right way to avoid hard coupling of unrelated components and avoid it within a set of related components if you can.

    Clear, readable , easy to follow code is going to be much easier to support over time :)
  • 0
    Wow, thanks! I'll keep that in mind.
Add Comment