我有一个connect()ed容器组件,我尝试将书籍列表写入我的redux商店。动作创建者可用作道具,状态映射到道具,但书籍列表永远不会进入redux商店。 readingList仍然是null而不是bookArray(动作创建者的传递参数)。有人能在这里发现问题吗?这里包含相关的片段:
import { setReadingList } from '../actions/index';
componentWillMount() {
this.props.setReadingList(bookArray);
}
function mapStateToProps(state) {
return {
readingList: state.readingList
}
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({ setReadingList : setReadingList }, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(ReadingList);
/*-----------*/
./actions/index.js
export function setReadingList(readingList) {
return {
type : "SET_READINGLIST",
payload : readingList
};
}
/*-----------*/
./reducers/index.js
import { combineReducers } from 'redux';
import readingList from './reading_list';
export const rootReducer = combineReducers({
readingList
});
export default rootReducer;
/*-----------*/
./reducers/reading_list.js
export default function(state = null, action) {
switch (action.type) {
case "SET_READINGLIST":
return action.payload;
default:
return state;
}
}
谢谢!
答案 0 :(得分:0)
尝试在componentDidMount中调用setReadingList而不是componentWillMount。还要确保bookArray在范围内。