[英]Why do I have to access my Redux state like "state.topicsReducer.topics"?
我已经使用 @reduxjs/toolkit 中的 createSlice 成功创建了一个 redux 存储,如下所示:
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
}
}
}
}
}
创建选择器时,我发现我必须执行以下操作:
export const selectTopics = state => state.topicsReducer.topics;
为什么我需要“topicsReducer”? 在我的课程中,我总是看到像“state.topics”这样的状态访问。 我在我的选项变量中搞砸了什么吗?
谢谢!
确保将reducers
对象传递给combineReducers(reducers)或configureStore,如下所示:
combineReducers({ topics: topicsReducer });
// or
configureStore({ reducer: { topics: topicsReducer } })
状态形状将是{ topics }
。 然后你可以创建选择器,如:
export const selectTopics = state => state.topics;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.