Flux + React: when to keep the state of visual components in storage

Sometimes this is not obvious: where should I keep the state of the React view, i.e. the active tab, the selected option, the value of toggler, is the confirmation flag entered?

There are actually two options:

  • Drop the action and save this data in the store
  • Store this data as view status

Which one is better? Are stores designed only for data from the server?


My thoughts:

  • It’s bad to keep this data in the store, because it leads to a chain of actions. Example: you need to load data on a tab, so you start an action NEW_TAB_SELECTEDand start a new action from the repository that processes it DOWLOAD_TAB_DATA.
  • Saving data avoids the first action ( NEW_TAB_SELECTED) and avoids chains of actions. But how to save the selected tab if I want to leave this view?
+4
source share
1 answer

Things that should be stored in the state of a component are things that only affect that component.

So, for example, if you have a component that opens to show more content, the flag isOpencan be kept in state because it is an internal component.

If the information is not part of the component (for example, the text of the message and whether the message has been read), it should be stored in the store and distributed through the application as necessary.

, this.state.

, , , , , toggler this.state. , - . , . , this.state.

, .

+4

Source: https://habr.com/ru/post/1617017/


All Articles