Recommended way to "listen" another change in a reducer #3198
Unanswered
mordonez-me
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
There are some situations where you need to "know" about something happened and avoid repeating useEffects in multiple components to react, for example a logout. Every time a user logs out from the system you want to reset the state of multiple stores, what would be the recommended way to do it? I have one in mind but I would like to get some opinions and recommendations.
Access the state directly with getState inside the logout (or a global reset function to call all of them, even logout)
//Inside logout call to each state and reset
useSomeState1.getState().reset()
useSomeState2.getState().reset()
useSomeState3.getState().reset()
// Using the recommended way to reset
reset: () => {
set(store.getInitialState())
},
There is a way to reset multiple states in the website but it just consideres all stores in the same place and I don't want to create the Set that stores the references as a global variable because it will feel a bit weird in a feature/domain based structure. Maybe using a wrapper for all stores could be an option, following the same pattern (wrapping store creation into another function)
I found this question #1654 but looks like it is a different perspective, like "extending" based on an action, I want to "react" based on an action, pretty similar but not exactly the same thing.
Beta Was this translation helpful? Give feedback.
All reactions