我有一个 api,它将列出当前用户拥有的所有访问权限,所以一旦 app.js 加载,我就会在 componentWillMount 中调用 api,所以基本上我有三个路由,家庭、用户列表、每个用户。所以主页是一个静态文本页面。
userslist 是我列出所有用户的组件,一旦您单击用户的编辑图标,它将带您进入每个用户组件中的用户详细信息。
问题在于,一旦 useraccessApi 解析并通过传递 useraccessApi 响应仅获取我应该调用 usersListApi 的数据,调用就是异步的。
我所说的快乐流程是第一个用户加载 localhost:3000/home 以便 useraccessApi 将调用并且 redux 有数据,因此在切换到 componenWillMount 上的 userslist 选项卡时它会起作用。但是如果用户直接选择 localhost:3000/userlist 它会在 componenWillMount 上抛出错误,因此将代码移动到 componentWillRecieveProps()。
那么我该如何解决这个问题。或者我应该使用 mergeProps 来解决它。
App.js
componenWillMount(){
this.props.userAccessApi()
}
UsersList
componentWillMount(){
const {currentUserAccess} = this.props
// if its a happy flow else it will be currentUserAccess is undefined
if(currentUserAccess){
this.props.usersListApi(currentUserAccess)
}
}
// inorder to resolve it
componentWillRecieveProps(nextProps){
const {currentUserAccess} = nextProps
if(currentUserAccess){
this.props.usersListApi(currentUserAccess)
}
}
const mapStateToProps = (state) => {
return {
currentUserAccess: state.access
}
}