【发布时间】:2016-04-15 18:14:56
【问题描述】:
假设有一个无状态的功能性UserProfile 组件,它显示给定url 的用户数据。假设它被connect(mapStateToProps, mapDispatchToProps)(UserProfile) 包裹。最后,假设有一个 reducer 可以归约为 state.userProfile。每当 url 发生变化时,我都需要重新初始化 state.userProfile,因此想到的解决方案是在 mapDispatchToProps 中这样做:
function mapDispatchToProps(dispatch, ownProps) {
dispatch(fetchUser(ownProps.userId))
return {
...
}
}
假设 thunked fetchUser 通过与当前状态进行比较来忽略重复调用,这是可以接受的做法吗?或者从这个 map 函数立即调用 dispatch 是否有问题?
【问题讨论】:
标签: javascript redux react-redux