Skip to content

how to update other store states from reducers? #562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bchenSyd opened this issue Dec 7, 2016 · 2 comments
Closed

how to update other store states from reducers? #562

bchenSyd opened this issue Dec 7, 2016 · 2 comments

Comments

@bchenSyd
Copy link

bchenSyd commented Dec 7, 2016

let's say my react application has 10 business pages and one error page; each page has its own store state , action creators, reducers..etc. Any unexpected result will lead user to the error page, with error message read from error store state. The store state at run-time is like below

{
   page1 : { key1: val1, key2:val2 },
   page2 : { key1: val1, key2:val2 },
   page3 : { key1: val1, key2:val2 },
   ............
   error: { error_msg:' unexpected error: specified user doesn\'t exist   '}
}

Now the question is how to set the error_msg in error state across the application.

If you use api frameworks like saga or CALL_API, the REQUEST_BEGIN/REQUEST_SUCCEEDED/REQUEST_FAILURE actions are dispatched within the api framework and therefore the first user code invoked after a failure is actually reducer code.
Since reducers are localised, they don't have access to other store states, so another action is needed to update error state. This isn't effective and looks a bit clumsy.

I was wondering if we can have a shared/communal store state, so that it can be updated directly from all reducers, or do we have better solutions?

@bchenSyd
Copy link
Author

bchenSyd commented Dec 7, 2016

ok.. looks like i've got it...

  1. dispatch SET_ERROR_MESSAGE with detailed error info before jumping to error age, which is what I'm doing
  2. modify my api framework to dispatch a SET_ERROR_MESSAGE right before it dispatches REQUEST_FAILURE, so that I don't have to set error message explicitly..

@jimbolla
Copy link
Contributor

jimbolla commented Dec 7, 2016

If you search the reactjs/redux issues, this has been discussed previously. The redux's team's stance on this is that combineReducers is meant to cover the most common use cases; but if you need more advanced behavior, such as passing global state, should be done in userland. There's probably an implementation in Mark's React/Redux Links list that does this already.

@jimbolla jimbolla closed this as completed Dec 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants