1

In the register component of my APP, the state is stored in a redux slice. After the user registers, this component will never be rendered again and thus the memory in its redux slice should be freed up for performance.

How can this be done?

Julien Kode
  • 5,010
  • 21
  • 36

3 Answers3

1

The performance gain from freeing up a tiny bit of memory is negligible if even measurable, don't bother.

timotgl
  • 2,865
  • 1
  • 9
  • 19
1

Can I dynamically add / remove slice of the state ?

Yes you can remove / add dynamically a slice of the state with redux, this is usually done for code splitting reasons

How to do it ?

You can follow this steps to achieve it: https://redux.js.org/usage/code-splitting

Note

I agree with @timotgl note, based on what you have in this slice this is negligible

Julien Kode
  • 5,010
  • 21
  • 36
0

Redux is generally used for global state. What you are describing is local state, and should probably be moved out of redux, and into local component state. You could keep your reducer, actions, and selectors, and just refactor them into a call to React.useReducer instead of redux.

Benjamin
  • 3,428
  • 1
  • 15
  • 25
  • But I want the state of these components to be global so that if the user navigate away and then back to the page, the page's state will be preserved – Bear Bile Farming is Torture Jul 16 '22 at 01:07
  • Persistent state is different than global state. You can persist the page state in location or localstorage. But redux is generally for global state that is needed by many components across your app. If it’s only used by one component then it isn’t really global state. – Benjamin Jul 16 '22 at 01:13
  • 1
    @BigCatPublicSafetyAct I think you could argue that what OP describes is global state. Let's say a register page and the user's country of origin (from a dropdown menu) is remembered. Wether that's persisted further (reloading the entire page) is a different question. However, how much data could this be in total? A few text fields' content? Micro-managing memory like that is just not something that's typically done in SPAs. The tradeoff in complexity alone is bad. Suddenly there is code to maintain that shuffles state in and out of the redux store. What for? – timotgl Jul 17 '22 at 09:43