Dealing with local state in react and redux

后端 未结 3 1922
时光说笑
时光说笑 2021-02-02 10:01

Is it OK to store local state in the state object when using react together with redux? Storing everything in the state tree via actions quickly becomes tedious. It

3条回答
  •  清酒与你
    2021-02-02 10:55

    As Tyrsius already mentioned - there are different opinions about this.

    For us - as a rule of thumb - we make sure to track everything with the application state which we would like to be able to see if we'd connect to some users current session remotely.

    If we don't care to see whether the mouse is hovered over some element, we might only use the components state for this (if we need the state then at all).

    We have only a few such cases in our scripts though, since we'd like to know exactly what the user sees in most cases for easier debugging.

    You're mentioning expanded/collapsed states for panels - we sometimes create components which handle this expanded/collapsed logic for us, so we don't have to write such reducers all the time for every panel we create.

    We can use these components like this:

    some content
    

    The panel component will make sure to track the panels active state within the application state. This way it's really easy to keep your code simple and not let it explode.

提交回复
热议问题