简体   繁体   English

React / Redux-一旦上一个动作完成就触发动作

[英]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: 我认为这应该是这样的:

  • Filter field is changed in the Filter component 在“过滤器”组件中更改了“过滤器”字段
  • Redux 'FILTER_UPDATE' action is fired, this triggers a post request to the server with the new filter settings 触发Redux'FILTER_UPDATE'操作,这会触发具有新过滤器设置的服务器后处理请求
  • Filter reducer updates the 'filter' state to reflect the new filter settings in React Filter Reducer更新“ filter”状态以反映React中的新过滤器设置

The next part is where I am getting stuck: 下一部分是我遇到的困难:

  • Once the update filter AJAX request is completed I need to trigger the action that fetches the list items again so the server can return the list based on the new filter settings 更新过滤器AJAX请求完成后,我需要触发再次获取列表项的操作,以便服务器可以根据新的过滤器设置返回列表

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. 谢谢大家。

At least you should wait server answer: 至少您应该等待服务器回答:

export function getList() {

    axios.get(API_ROOT+'/item').then(function (response) {
       return {
           type: GET_LIST,
           payload: response
       };
    });
}

See axios examples 请参阅axios示例

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM