【发布时间】:2022-01-13 17:08:39
【问题描述】:
我正在使用 react + redux + redux saga
我在渲染页面时遇到了问题(GET 调用) 调用应该是这样的:
- 构造函数
- 渲染()
- componentDidMount
- 渲染()
但我刚刚到达componentDidMount,mapDispatchToProps 正在调度操作,API 调用正在工作,通过它从服务器获取响应并将数据更新到状态。
但是它在某处丢失了,我的组件甚至没有重新渲染。
直到减速器,我正在获取要返回的数据action.items。
itemReducer.js
const itemReducer = (state = initialState, action) => {
switch (action.type) {
case types.GET_ALL_ITEMS_SUCCESS:
console.log("itemReducer-----", action.items); //getting the data over here
return action.items;
default:
return state;
}
};
itemPage.js(组件)
class ItemsPage extends React.Component {
componentDidMount() {
this.props.loadItems();
}
render() {
const { items } = this.props; // not even it renders, so not getting data
...
return (<div>...</div>);
}
}
const mapStateToProps = (state) => {
return {
items: state.items,
};
};
const mapDispatchToProps = (dispatch) => {
return {
loadItems: () => dispatch(loadAllItemsAction()),
};
};
export default connect(mapStateToProps, mapDispatchToProps)(ItemsPage);
请给一些建议,提前致谢 :D
【问题讨论】:
标签: reactjs redux react-redux axios redux-saga