最近在学习React,首次尝试用Taro写项目。
用Redux的时候,在页面中dispatch改变state中的数据
个人理解如下:
使用connect将state中的数据作为props绑定到组件上,
@connect(({ course }) => ({
course
}), (dispatch) => ({
dispatchFetchCourse() {
return dispatch(fetchCourse())
}
}))
当调用dispatch的时候,修改了props,
async componentDidMount() {
await this.props.dispatchFetchCourse();
}
所以应该会调用componentWillReceiveProps
,
componentWillReceiveProps(nextProps){
console.log('this.props', this.props.course.lastUpdated)
console.log('nextProps', nextProps.course.lastUpdated)
}
并且在这个生命周期中,应该可以看到当前的props和即将更新的props,但是现在console查看的都是更新后的props。
数据中的lastUpdated是发起dispatch后才会添加到state中的,但是现在在更新前查看却可以看到更新后的,所以有点懵逼。