-1

Which one of the two scenarios is preferred?

  1. Use state variables to store the value from the backend/server and use it directly in all the components(Eg: DataTable) present on the Page.

  2. While calling the APIs, that data will load in Redux store. So instead use the data from the redux store directly in all the components.

  • 1
    Does this answer your question? [When do I choose React state Vs Redux Store](https://stackoverflow.com/questions/41584647/when-do-i-choose-react-state-vs-redux-store) – Moistbobo Sep 20 '21 at 06:25

3 Answers3

0

Case 1 : When to use Local React State

React state is stored locally within a component. When it needs to be shared with other components, it is passed down through props. In practice, this means that the top-most component in your app needing access to a mutable value will hold that value in its state. If it can be mutated by subcomponents, you must pass a callback to handle the change into subcomponents.

Case 2 : When to use Redux state management

When using Redux, state is stored globally in the Redux store. Any component that needs access to a value may subscribe to the store and gain access to that value. Typically, this is done using container components. This centralizes all data but makes it very easy for a component to get the state it needs, without surrounding components knowing of its needs.

Hithesh kumar
  • 1,008
  • 9
  • 25
0

Case 2 should be preferred.

When the data is used in multiple components and is coming from the backend, you should store it in a redux state.

Faisal Rehman
  • 43
  • 1
  • 2
  • 5
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 20 '21 at 08:56
0

Keep any state that can be called "Application State" in the redux. If that object somehow is defining some part of your application and not just a state of a component(e.g. showSpinner, isEditMode, etc) then it's your applications state and should be kept in the redux store. That's kind of my definition of difference. Also, note that you should always keep only JSON serializable object in your redux store. Also, I mostly consider data from backend to be a part of "Application State".

So I think that your data should be stored in redux. Technically you have on advantage when using redux store in your case as redux triggers component updates only when the data is changed. So using it here might give you some performance benefits as well.

hakobpogh
  • 632
  • 6
  • 13