[英]React/Redux - Trigger action once previous action has completed
I'm building my first app with React + Redux and I have come across an issue and I'm not sure of the best way to deal with it. 我正在使用React + Redux构建我的第一个应用程序,但遇到了一个问题,我不确定如何解决它的最佳方法。
I have a "Filter" and a "List" component. 我有一个“过滤器”和“列表”组件。 The list is just a list of records and the contents of the list is determined by the filter settings. 该列表仅是记录的列表,并且列表的内容由过滤器设置确定。 This is how I think it should work: 我认为这应该是这样的:
The next part is where I am getting stuck: 下一部分是我遇到的困难:
Here's what I have tried. 这是我尝试过的。 This is the function in the filter component which fires the action: 这是触发操作的筛选器组件中的函数:
updateFilter(event) {
const field = event.target.getAttribute('name');
const value = event.target.value;
this.props.updateFilter(field,value); // Updates the filter
this.props.getList(); // Fetches the new filtered list
}
The problem is that the getList()
action is fired too quickly. 问题是getList()
操作触发得太快。 I need to wait for the updateFilter()
AJAX request to complete so that the server is aware of the filter settings when it resends the items. 我需要等待updateFilter()
请求完成,以便服务器在重新发送项目时知道过滤器设置。
Which is the best way to trigger the getList()
action but only once the updateFilter()
AJAX request is completed? 最好仅在updateFilter()
AJAX请求完成getList()
触发getList()
动作的最佳方法是什么?
Here are the two actions updateFilter()
and getList()
: 这是两个操作updateFilter()
和getList()
:
// Update filter
export function updateFilter(field,value) {
const response = axios.post(API_ROOT+'/filter/update',{
field: field,
value: value
});
return {
type: UPDATE_FILTER,
payload: {field, value}
}
}
// Get items
export function getList() {
const response = axios.get(API_ROOT+'/item');
return {
type: GET_LIST,
payload: response
}
}
Here are the relevant parts from the List and Filter reducers: 以下是列表和过滤器归约器的相关部分:
// Filter reducer
case 'UPDATE_FILTER':
return update(state,{
[action.payload.field]: { $set: action.payload.value }
});
// List reducer
case 'GET_LIST':
return action.payload.data;
Thank you all in advance. 谢谢大家。
There are many lib and solution to do that, you can start by looking at redux-thunk that can be suited for your implementation. 为此,有很多lib和解决方案,您可以先从适合于您的实现的redux-thunk入手。 It aims to manage async action that can fire another action. 它旨在管理可以触发其他操作的异步操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.