-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
WIP: createSlice() revision proposal #109
Conversation
Deploy preview for redux-starter-kit-docs ready! Built with commit 9024664 https://deploy-preview-109--redux-starter-kit-docs.netlify.com |
@markerikson Any thoughts on this? |
My attention is unfortunately very much focused on React-Redux stuff atm, so no, I haven't had time to think through this. (Also just got back from a 3-week business trip, and things are pretty hectic at work atm.) Skimming this, I can get behind I agree the slice selector is sort of appendix-ish in functionality atm, but I'd still like to find some kind of better approach for generating selectors somehow. |
I like it. I can also respect wanting to keep |
@agmcleod : I think that's where the https://redux-starter-kit.js.org/api/createslice#extrareducers const globalAction = createAction("doStuff");
const sliceA = createSlice({
slice : "sliceA",
extraReducers : {
[globalAction]: (state, action) => {}
}
});
const sliceB = createSlice({
slice : "sliceB",
extraReducers : {
[globalAction]: (state, action) => {}
}
}); |
Oh nice, yeah i missed that. I read up on them before, but i wasn't quite sure what they were for 😬 |
Looking at this again. As mentioned, I agree with changing Also, I don't want to pass Is there an actual benefit to making the "slice" be the reducer function itself? |
@markerikson Thanks for the feedback. No problem with reverting the The one practical benefit of "the slice is the reducer" is that you can pass the slice directly to functions like |
To further the discussion about how to evolve
createSlice
(#91), I whipped I what I think would be an improved, but slightly different take on the slice concept.Changes
The slice selector is removed. The selector makes strong assumptions about where the slice is mounted in the state tree, hindering comparability. It's also the least useful feature in that it saves very little code and is often not really needed. There is still a slice name parameter, but it's only used for action names-acing.
The slice is the reducer. Instead of representing the slice as an object that has a
reducer
property, now the slice is the reducer function itself. The action creators are still available asslice.actions
. What's nice about this change is that you can pass slices directly tocombineReducers
or other reducer-composing functions.Minor parameter naming tweaks. The
slice
parameter is nowname
, andreducers
was renamed toactions
to make it more different fromextraReducers
.Example
Possible Improvements
Add a
combineSlices
function that combines slices into a reducer likecombineReducers
, but reads the object keys from the slice names.Pass payloads directly to
actions
functions.createSlice
generates the action creators, so we know that thepayload
is the only interesting thing about them. It's just easier for the user if we let them specify(state, payload) => state
functions instead of(state, action) => state
.Feedback Welcome
This is meant as a first draft towards better slices. If you like the general direction this is moving towards, we can develop this further. Otherwise, let's find out what's good or bad about this and move on.