I've successfully created a redux store using createSlice from @reduxjs/toolkit as follows:
const initialState = {
topics: {
'123': {
id: '123',
name: 'example topic',
icon: 'icon url',
quizIds: ['456']
}
}
}
const options = {
name: 'topics',
initialState: initialState,
reducers: {
addTopic: (state, action) => {
return {
...state,
topics: {
...state.topics,
[action.payload.id]: action.payload
}
}
}
}
}
When creating a selector I found that I have to do the following:
export const selectTopics = state => state.topicsReducer.topics;
Why do I need the "topicsReducer"? In my course I've always seen state accessed like 'state.topics'. Did I mess something up in my options variable?
Thank you!
Make sure you pass the reducers
object to combineReducers(reducers) or configureStore like this:
combineReducers({ topics: topicsReducer });
// or
configureStore({ reducer: { topics: topicsReducer } })
The state shape will be { topics }
. Then you can create selector like:
export const selectTopics = state => state.topics;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.