-1

In our application we use redux and store out the whole application state in a global state. But I'm just interested is it correct to store some components's data also in own state? Thus we will have part of data in a global redux state and part of data in component's own state.

Please share your thoughts about it.

Erik
  • 14,060
  • 49
  • 132
  • 218
  • This is a little open. What are you trying to do with that component? – christopher Dec 05 '16 at 09:58
  • My component is a form with some fields and CRUD table – Erik Dec 05 '16 at 10:01
  • And what does it do that could use some state? – christopher Dec 05 '16 at 10:02
  • When adding the new table rows they not in a database yet. So the user may add three ones and submit a bunch of data. For these purposes I thought to use component's state – Erik Dec 05 '16 at 10:05
  • If they add the rows without saving and then leave and return, are they expecting them to still be there? – christopher Dec 05 '16 at 10:06
  • No, if the user leaves the form they should disappear – Erik Dec 05 '16 at 10:08
  • Then it might be a good idea for your component to be stateful! You get the state clearing for free with it and you don't need to worry about writing reducers for handling the new data. – christopher Dec 05 '16 at 10:11
  • BUT it might be an idea to split the stateful bit out into a parent container component and pull out as many stateless components as possible, just to minimise the impact of that state. – christopher Dec 05 '16 at 10:12
  • I agree with @christopher – locropulenton Dec 05 '16 at 12:20
  • Possible duplicate of [React-Redux: Should all component states be kept in Redux Store](https://stackoverflow.com/questions/35328056/react-redux-should-all-component-states-be-kept-in-redux-store) – Michael Freidgeim May 10 '19 at 21:41

2 Answers2

1

Firstly, it's not wrong to combine local state and redux-state, but it's not fully correct also. It depends your needs.

In some of my components, displayed values and redux-state values are a bit different. For example usage of my custom NumberField, values are displayed as 123.123,567 but binded to redux-state as 123,123.456.

In addition, you can use local state to manipulate some values or hold some helper states etc.

If It's not problem to trace redux-state and your business flow, feel free about using local state, but be careful!

There is another discussion here

Tugrul
  • 1,760
  • 4
  • 24
  • 39
0

Combining the two kinda defeats the purpose of using Redux. It's there to serve data from it's own 'state'. The best way to use Redux is to only listen for the data that you need and is narrowed down to the nearest parent. That way your app will be more performant because it's not re-rendering every time data somewhere else re-renders.

Dan
  • 1