If I have a state in reducer in Redux, should I have a 'local' state in a function component? Or I only should use a state from reducer? More clearly... If I am using Redux should I use React hooks like (useState)?
Asked
Active
Viewed 56 times
0
-
So When i need a state which be use for more than one component, I should write this state in reducer and state which be use for only one component should be write in a that component not in the reducer? – Bachi Nov 02 '20 at 14:11
-
The last statement is true. The first statement, not always. There are more ways to pass data between components (such as [HOC](https://reactjs.org/docs/higher-order-components.html), props propagation). – Mosh Feu Nov 02 '20 at 14:27
1 Answers
0
There is no “right” answer for this. Some users prefer to keep every single piece of data in Redux, to maintain a fully serializable and controlled version of their application at all times. Others prefer to keep non-critical or UI state, such as “is this dropdown currently open”, inside a component's internal state.
Using local component state is fine. As a developer, it is your job to determine what kinds of state make up your application, and where each piece of state should live. Find a balance that works for you, and go with it.
Some common rules of thumb for determining what kind of data should be put into Redux:
- Do other parts of the application care about this data?
- Do you need to be able to create further derived data based on this original data?
- Is the same data being used to drive multiple components?
- Is there value to you in being able to restore this state to a given point in time (ie, time travel debugging)?
- Do you want to cache the data (ie, use what's in state if it's already there instead of re-requesting it)?
- Do you want to keep this data consistent while hot-reloading UI components (which may lose their internal state when swapped)?

Michael S. Molina
- 722
- 4
- 9
-
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/27528627) – рüффп Nov 02 '20 at 14:32
-
@рüффп Thank you for your feedback. Edited to include essential parts. – Michael S. Molina Nov 02 '20 at 14:42