【发布时间】:2019-03-04 21:12:28
【问题描述】:
我的 Redux mapStateToProps 函数出现问题,抛出一个错误,指出 state.currentOpportunities 未定义。奇怪的是,organizationReducer 中定义的初始状态只能在 state._root.entries1.organization.currentOpportunity 而不是 state.organization.currentOpportunity 当我 console.log 我 console.log主 index.js 文件中的状态
主 App.js 文件
const initialState = {}
const history = createHistory();
const store = configureStore(initialState, history);
const MOUNT_NODE = document.getElementById('app');
const render = messages => {
ReactDOM.render(
<Provider store={store}>
<LanguageProvider messages={messages}>
<ConnectedRouter history={history}>
<App />
</ConnectedRouter>
</LanguageProvider>
</Provider>,
MOUNT_NODE,
);
};
organizationReducer.js
const initialState = {
currentOpportunities: [],
expiredOpportunities: []
};
export default function (state=initialState, action) {
switch(action.type) {
case FETCH_ORGANIZATION_OPPORTUNITIES:
return {...state, data: action.payload}
case FETCH_CURRENT_OPPORTUNITIES:
return{...state, currentOpportunities: action.payload}
case FETCH_EXPIRED_OPPORTUNITIES:
return{...state, expiredOpportunities: action.payload}
default:
return state
}
}
我的根减速器文件
export default function createReducer(injectedReducers) {
return combineReducers({
organization: organizationReducer,
...injectedReducers
})
}
index.js 文件
const mapStatetoProps = (state) => {
console.log(state)
return {
currentOpportunities: state.organization.currentOpportunities,
expiredOpportunities: state.organization.expiredOpportunities
};
}
export default connect(mapStatetoProps, actions)(OpportunitiesPage);
有人知道这里会发生什么吗?
【问题讨论】:
-
你能访问organization reducer吗?我的意思是你能用 Redux-dev-tools 检查它,看看里面有什么吗?
-
我检查了 Redux-dev-tools,它看起来像在 state 选项卡下的 @@init 下,它显示了 currentOpportunities 和 expiredOpportunities 的组织初始化值作为来自 reducer 的空数组。
-
@riku12764,你的
action.payload的结构是什么?也许您还想仔细检查为什么FETCH_ORGANIZATION_OPPORTUNITIES中有一个data键。 -
你注入的Reducers里面是什么?
-
@George.S,目前injectedReducers 内部没有任何内容(只是一个占位符,直到我让它在最基本的级别上工作)
标签: javascript reactjs redux react-redux