Say I have an application for keeping track of university courses. Each course has some attributes like name time notes id. In the application, the user can add/remove courses and edit their attributes. How would I structure a redux store for this?
My first thought is to make the state a mapped object like so:
interface Course {
id: string;
name: string;
time: string;
notes: string;
}
interface StoreState {
[id: string]: Course;
}
where each entry in the state corresponds to a course instance. Then, when the user adds a new course, I add a new entry to the state
const slice = createSlice({
name: 'courses',
initialState: {},
reducers: {
addCourse: (state, action) => {
state[action.payload.id] = {
id: action.payload.id,
name: action.payload.name,
...
...
}
},
updateNotes: (state, action) => {
state[action.payload.id].notes = action.payload.notes
}
...
...
}
})
This is not ideal because I have to redundantly use the course id in every case reducer to update the correct course. I'd like to have a state object in the store for each course and write my case reducers like this:
updateNotes: (state, action) => {
state.notes = action.payload.notes
}
I shouldn't have to worry about which course I'm updating because they all function the same way. Is there a way to dynamically create states for each course that share the same reducer logic?
You need to implement a dynamic version of redux's combineReducers().
There seems to be a package that does this: https://www.npmjs.com/package/redux-injector
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With