-
-
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
Find a way to cut getReducer and replaceReducer from the API #350
Comments
Here is my proposal. I am sure it has some issues. Expose reducer as a simple function to Higher Order Stores. Higher order stores would be created like this: createStore(middleware, devtools, persistState); Higher Order Stores would have the following signature: function higherOrderStore({ reducer, dispatch, getState, subscribe }) {
// wrap components
return {
reducer: newReducer,
dispatch: newDispatch,
// ...
};
} The create store function would have the following signature createStore(...funcs) {
const store = new Store();
const finalStore = compose(...funcs, store);
finalStore.dispatch({ action: '@@init' });
return store;
} This allows the store to be very flexible. This is just off the top of my head. Haven't thought of all the implications of this change. It definitely would solve #376 though. |
Interesting, thanks. On the first sight I like it, but I haven't thought through the implications. |
@taylorhakes Can you hack up a proof of concept of this in a branch? |
+1 |
Sure, I can do it this weekend. |
I have one more proposal. The idea is that EDIT: plus a Here's how I'd implement it: https://github.com/acstll/chopped-redux/blob/next/index.js#L32-L40 So the API would be this: let store = createStore(reducer, initialState)
// Create a new store replacing `reducer` but keeping state and listeners.
store = store(nextReducer) With this, I think React bindings code would be like this: Now componentWillReceiveProps(nextProps) {
const { store } = this.state;
const { store: nextStore } = nextProps;
if (store !== nextStore) {
const nextReducer = nextStore.getReducer();
store.replaceReducer(nextReducer);
}
} After componentWillReceiveProps(nextProps) {
const { store } = this.state;
const { store: nextStore } = nextProps;
if (store !== nextStore) {
store = store(nextStore.reducer)
}
}
// the before EDIT `store = nextStore(null, store.getState())` would lose its original listeners I have to say I actually don't see any problems with the |
@taylorhakes what you get from the first
I don't think so 😁 If you need to chain-wrap the From #376:
Maybe the problem is with |
Brilliant 👌 @gaearon you could even expose the |
Do you mean expose on the I'll think about how to implement code splitting with the same |
@gaearon yes I meant that.
Looking forward to it. 👍 |
I think this proposal is great, but it doesn't address the issue discussed here #376 (it doesn't necessarily have to). I would like to reopen that issue if this is the road we decide. Sorry I wasn't able to implement my proposal above. I have been really busy with other things. I will try to find some time this week. |
I was a bit disoriented by the approach taken in |
I've been reading this thread, and the related ones with interest. I'm new to Redux, though code splitting is definitely something I've used in the past with success, and likely will continue to do so going forward. Can I ask why you're looking to get rid of replaceReducer? It looks like a direct, simple and effective solution to this. Are there deeper issues with it I'd need more experience with Redux to be aware of? |
I think I'm satisfied with removing |
根据 reduxjs/redux#350 及文档原文, API 已将 `getReducer()` 移除,`replaceReducer(nextReducer)` 得以保留
@gaearon Sorry for commenting on a closed issue, but I ended up here from issue #37, about code-splitting in large apps, which I'm trying to do now, but that issue and this one is closed but I'm not entirely sure that the code-splitting issue is solved or is it? |
@hummlas The current solution is to use |
@gaearon Wow, thanks for the quick reply! I thought I saw somewhere that |
It was un-deprecated later :-). |
HI, any benevolent soul that can provide an example of its implementation? |
Hi @gaearon - is it expected that after a call to replaceReducer, a default call to dispatch would be made with an undefined state, so the new default can be set? It doesn't seem to happen. As it is, the workaround is trivial store.replaceReducer(reducer); with case INITIALIZE: I'm just curious if I'm missing something. |
Of course, you'll only have the initial state for parts of the tree that didn't have any state associated with them. This is useful when you're doing hot reloading, or when you're adding a new code-split reducer under an existing reducer tree. If you want to clean up all the state when running |
That clarifies things perfectly - thank you much! I'm still quite new to react and redux. |
Pretty simple example for those who may be interested. |
They are used for hot reloading by react-redux and code splitting. However the solution feels wrong and ad-hoc to me.
Something like a store enhancer that would wrap the reducer a la devTools seems like the way forward. This way hot reloading would be opt in, but much more reliable.
Whatever the solution may be, it has to solve the following issues to be considered:
#37 #301 #340 #346 #348 reduxjs/redux-devtools#68
Ideas welcome!
The text was updated successfully, but these errors were encountered: