-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
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
Expose currentReducer from store through getReducer() #4107
Comments
I'm not quite sure I understand the specific use case you're describing. When you say "initializes the Redux context for us", do you actually mean a Redux store? As in, the other lib creates the store internally and gives it to you, and thus you never have a chance to see what the reducer is? |
Yes we are not able to see the source code of the reducer. But we would like to override some behavior of the reducer. |
The problem is the reducer is only half the issue there. There may be middleware or enhancers also applied to the store, which can significantly alter its behavior before ever getting to the reducer function. |
That's true, but the store is already created, so we cannot add the enhancer before hand |
I can see at least two potential use cases for having this:
Agreed that this doesn't take middleware into account at all, but then again that's not something you can customize later even if you did make the store originally. Obviously this is trivial at an implementation level - just I'm always a bit hesitant when it comes to adding things to the public API at this point, but I can't immediately think of any reasons why this would be a bad idea exactly. |
It's worth noting that we actually did have |
Something like this would be great for code-splitting. I have been hacking around this afternoon to find a clean way to load a feature slice when a lazy loaded component is routed to. Here's something I have come up with so far. This specific part of the code could benefit from having |
Hmm. I'm actually a bit confused by this piece of the snippet: useEffect(() => {
const existingReducers = store.getCurrentReducer();
const updatedReducers = {...existingReducers, ...{ [slice.name]: slice.reducer}};
store.replaceReducer(combineReducers(updatedReducers);
}, [slice, store]) I realize this is effectively pseudocode because The underlying Redux store takes a reducer function. Once that function has been created, there's no way to figure out how it might have been created in the first place, and what the store itself tracks is a reference to that function. This snippet is written assuming that the store is tracking an object full of slice reducer functions, which isn't what it does. Per the Code Splitting page in the docs, this is why it's necessary / recommended to keep around an object with the original slice reducers for later reuse, so you can re-create the root reducer using those same slice reducers as the input. But, there's no way for So, in that sense, I don't see how having a |
Thanks for the feedback Mark. I’ll dig back into the code and see if I can find another solution. I’m fairly new to react, been knee deep in Angular/NgRx for years. |
Sure, no problem! :) And feel free to ping me or the other maintainers over in Reactiflux if you'd like to chat in more detail there. |
@wesleygrimes Yeah you will get the whole reducer function out from the store, and you will need to wrap it as another function. We cannot unpack the current reducer. |
This is not a duplicate of #1246 because we have a different type of use case
New Features
I would like to get the currentReducer using the store.getReducer()
What is the new or updated feature that you are suggesting?
The createStore in https://github.com/reduxjs/redux/blob/master/src/createStore.ts will have a new method
and get exposed in store object
Why should this feature be included?
Our project uses a closed source proprietary library which initializes the redux context for us.
However we would like to override some part of the reducer logic, but we cannot modify the source because we don't have it or use other libraries to do it.
Since we cannot initialize the reducer by ourselves, we can only resort to extracting the reducer from the store. This is a different situation than #1246 as they have the full access of the reducer when passing into createStore.
So we would like to add this method to get the currentReducer out, and wrap it to change some behavior, and pass it back to replaceReducer.
What docs changes are needed to explain this?
We will need to modify the Store Methods part of the document to explain the new method https://redux.js.org/api/store#store-methods
Also I can add some of the example code in the documentation to illustrate how to use together with replaceReducer to override some of the reducer logic.
The text was updated successfully, but these errors were encountered: