我想知道如何在React Redux中分派具有不同参数的动作。
在format.js中,具有不同参数的queryData
由callFetch
方法调用。道具分发后,在render方法中,将在道具memberData
中接收数据,
我收到状态为memberData
的回复Inactive
,但如何同时显示Active
和Inactive
,
// format.js
componentDidMount(){
this.callFetch();
}
callFetch(){
this.props.dispatch(queryData(this.createFetchUrl("Active")));
this.props.dispatch(queryData(this.createFetchUrl("Inactive")));
}
createFetchUrl(status) {
const body ={
position: "civil",
status: status
}
return body;
}
render(){
const { memberData } = this.props.data
return(
<div>
Active
<p>{memberData}</p>
Inactive
<p>{memberData}</p>
</div>
)
}
//actions.js
export const queryDeviceByMember = payload =>
queryApi({
request: CONSTANTS.DATA,
url: `api/details`,
encode: false,
success: CONSTANTS.DATA_SUCCESS,
failure: CONSTANTS.DATA_FAILURE,
...payload
});
//reducer.js
case CONSTANTS.DATA_SUCCESS:
case CONSTANTS.DATA_FAILURE:
return {
...state,
memberData: data,
apiPending: false,
errormsg: errormsg,
servererror: servererror || ""
};
在active
和inactive
状态下都接收到相同的响应,因为它覆盖而不覆盖方法
[{
id: "20",
name: "xxx",
device: "ios"
},
{
id: "10",
name: "yyy",
device: "ios"
}]
您正在覆盖memberData
中的数据,请考虑以下步骤:
active
和inactive
情况下拆分减速器>export const getActiveMembers = state => state.activeMembers;
export const getInactiveMembers= state => state.inactiveMembers;
mapStateToProps(state)
从您的状态获取数据:function mapStateToProps(state) {
return {
activeMembers: getActiveMembers(state),
subscription: getInactiveMembers(state)
};
}
render(){
const { memberData } = this.props.data
return(
<div>
Active
<p>{memberData}</p>
Inactive
<p>{memberData}</p>
</div>
)
}
P.S:我将使用一个API调用来获取所有members
,然后使用化简器将它们分类为inactive
和active
。