Suggestion: add syntax or examples for nested slices
See original GitHub issueCurrently redux-toolkit
does not provide (or I couldn’t find) an integrated way to define nested slices/reducers.
To split some slice logic into smaller slices, I’m currently doing something like this:
import { createSlice } from '@reduxjs/toolkit'
// import the reducer, initial state and relevant actions from the desired slice
import otherReducer, {
complexAction,
initialState as otherReducerInitialState
} from './otherReducer'
const myReducer = createSlice({
name: 'myReducer',
initialState: {
someData: 'hello',
// manually bind initial state
someComplexData: otherReducerInitialState
},
reducers: {
doStuff: (state, action) => {
// ...
},
},
extraReducers: {
// manually bind each action
[complexAction]: (state, action) => {
// manually foward the state and action to the nested reducer
state.someComplexData = otherReducer(state.someComplexData, action)
},
}
})
export const { doStuff } = myReducer.actions
export default myReducer.reducer
The above example works, but it’s a lot of boilerplate code, and I can’t shake the felling there should be a better way to describe nested slices.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:25
- Comments:13
Top Results From Across the Web
How do I update a nested Item in a different reducer (slice ...
You want to iterate over every scene and remove the deleted role from the roles array.
Read more >Usage Guide - Redux Toolkit
In this example, both users and posts would be considered "slices". Both of the reducers: "Own" a piece of state, including what the...
Read more >2359-subslice-pattern-syntax - The Rust RFC Book
Permit matching sub-slices and sub-arrays with the syntax .. . Binding a variable to the expression matched by a subslice pattern can be...
Read more >Practical Redux, Part 11: Nested Data and Trees
This time, we're going to add creation of new entities, implement loading of nested relational data, and display that nested data in a...
Read more >Python - Slicing Strings - W3Schools
You can return a range of characters by using the slice syntax. Specify the start index and the end index, separated by a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I made a
combineSlices
helper function to make it easy to adapt @agrinko’s suggestion to any use case (note that it requires that you installreduce-reducers
):Note that you need to pass it the initial state from the parent slice so that it knows what keys to make dummy no-op reducers for. Thus, here’s an example of how it could be used:
Yeah,
combineReducers
is opinionated that way - it assumes all fields get handled by individual slice reducers, which is really more meant for top-level chunks of state vs individual fields.We’ve had requests to remove that warning, but opted not to do so. Note that there’s a million re-implementations of
combineReducers
out there, so it’s easy to use one that doesn’t have that warning if you’d like.