依赖于来自另一个状态的数据的动作
Action dependant on data from another state
我有一个带有 3 个减速器的 react-redux 应用程序:clientPrivileges
、userFilter
和 userData
作用于我的商店。
table 组件用于显示每个用户的数据,下拉组件用于为特定用户过滤此 table。当为特定用户选择下拉列表时,我需要调用后端来检索数据。这是与此相关的操作:
selectUser(userId, dispatch) {
api.getUserData(userId, accessTypeId, (data) => {
dispatch(userActions.update(data));
});
return{
type: selectUser,
userId
}
}
但是你可以看到我有一个名为 accessTypeId
的参数需要发送到后端以及 userId
。该值已在登录应用程序时使用 clientPrivileges
reducer 在商店中设置。
除了在 mapStateToProps
中将 accessTypeId
设置为下拉组件的 prop 之外,我看不到任何其他方法。然后在组件本身:
this.props.users.map(u => {
function onClick()
{
selectEndUserGroup(u.id, this.props.accessTypeId);
}
return <div id={"filter_group_"+u.id} key={u.id} onClick={onClick}>{name}</div>
但现在我已经用 accessTypeId
属性 破坏了我的通用下拉组件。我应该怎么做?
如果我没理解错的话,您希望您的操作能够访问存储在 Redux 状态中的值,是吗?
Redux-Thunk 处理得很好。代码看起来像这样;
selectUser(userId) {
return function(dispatch, getState){
var accessTypeId = getState().[PATH TO REDUX STATE ELEMENT]
api.getUserData(userId, accessTypeId, (data) => {
dispatch(userActions.update(data));
});
dispatch({
type: selectUser,
userId
})
}
}
我有一个带有 3 个减速器的 react-redux 应用程序:clientPrivileges
、userFilter
和 userData
作用于我的商店。
table 组件用于显示每个用户的数据,下拉组件用于为特定用户过滤此 table。当为特定用户选择下拉列表时,我需要调用后端来检索数据。这是与此相关的操作:
selectUser(userId, dispatch) {
api.getUserData(userId, accessTypeId, (data) => {
dispatch(userActions.update(data));
});
return{
type: selectUser,
userId
}
}
但是你可以看到我有一个名为 accessTypeId
的参数需要发送到后端以及 userId
。该值已在登录应用程序时使用 clientPrivileges
reducer 在商店中设置。
除了在 mapStateToProps
中将 accessTypeId
设置为下拉组件的 prop 之外,我看不到任何其他方法。然后在组件本身:
this.props.users.map(u => {
function onClick()
{
selectEndUserGroup(u.id, this.props.accessTypeId);
}
return <div id={"filter_group_"+u.id} key={u.id} onClick={onClick}>{name}</div>
但现在我已经用 accessTypeId
属性 破坏了我的通用下拉组件。我应该怎么做?
如果我没理解错的话,您希望您的操作能够访问存储在 Redux 状态中的值,是吗?
Redux-Thunk 处理得很好。代码看起来像这样;
selectUser(userId) {
return function(dispatch, getState){
var accessTypeId = getState().[PATH TO REDUX STATE ELEMENT]
api.getUserData(userId, accessTypeId, (data) => {
dispatch(userActions.update(data));
});
dispatch({
type: selectUser,
userId
})
}
}