[英]is that Ok to call multiple actions at the same time?
I have multiple reducers and I need to send separate actions for each of them on a click so this is what I did: 我有多个reducer,单击一次时需要针对每个reduce发送单独的动作,所以我要做的是:
<TouchableOpacity
style={[styles.button, styles.edit]}
onPress={()=>{
this.props.toggleCurrentTask(this.props.task);
this.props.triggerUpdating(id);
}}
>
<Text>Edit</Text>
</TouchableOpacity>
but the toggleCurrentTask
action didn't work and I was struggling for a while, at the end just replacing them solved my problem! 但是
toggleCurrentTask
操作无法正常工作,我苦苦挣扎了一段时间,最后只需更换它们就解决了我的问题! like this: 像这样:
onPress={()=>{
this.props.triggerUpdating(id);
this.props.toggleCurrentTask(this.props.task);
}}
So I thought maybe calling two actions at the same time is a bad practice. 所以我认为也许同时调用两个动作是一个不好的做法。 If that's the case, what should I do instead?
如果是这样,我该怎么办?
This is fine, but you can also have different reducers react to the same action (type): In your example the reducer storing the updating
state could change its state when a toggleCurrentTask
action is dispatched: 很好,但是您也可以使不同的reducer对相同的动作(类型)做出反应:在您的示例中,当调度
toggleCurrentTask
动作时,存储updating
状态的reducer可以更改其状态:
const TOGGLE_CURRENT_TASK = 'task/TOGGLE_CURRENT_TASK';
/* update reducer */
const updatingState = {
isUpdating: false
}
const updatingReducer = (state = updatingState, action) => {
switch (action.type) {
case TOGGLE_CURRENT_TASK:
return {
...state,
isUpdating: true
}
}
}
/* task reducer */
const taskState = {}
const taskReducer = (state = taskState, action) => {
switch(action.type) {
case TOGGLE_CURRENT_TASK:
return {
...state,
activeTasks: action.task
}
}
}
IMO this keeps your React component cleaner and scales better, imagine your have to dispatch five different actions in the event handler instead of just two. IMO可以使您的React组件更整洁,扩展性更好,想象一下您必须在事件处理程序中分派五个不同的动作,而不仅仅是两个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.