【问题标题】:React Redux async actions handlingReact Redux 异步操作处理
【发布时间】:2016-10-20 14:32:45
【问题描述】:

我有处理输入 onChange 的通用函数:

export function onChange(e) {
    return (dispatch) => {
        const payload = {};
        payload[e.target.id] = e.target.value;
        dispatch({
            type: e.target.name,
            payload
        })
    }
}

在某些输入发生更改后(例如,设置每个表的行数我想获取 30 或 60)我需要触发 fetch 函数。

但是当我在 onChange 之后立即调用 fetch() 函数时 - 在 onChange 更改属于 oof 请求 url 一部分的道具之前开始获取 - 请求不正确。

onChange 改变 redux 状态后如何处理这种异步操作以获取 strt

编辑
我要调用的第二个函数是

export function searchContacts(e, props) {

return (dispatch) => {
    let propsReceived = props.allContacts;

    let searchString = "?page="      + propsReceived.paginate +
                       "&orderBy="  + propsReceived.order    +
                       "&sortedBy=" + propsReceived.sort     +
                       "&limit="    + propsReceived.limit;
    if(e){
        let search = e.target.value;
        searchString += "&search=first_name:" + search +";first_name:"+ search;
    }
    let url = ALL_CONTACTS + searchString;
    return  dispatch(getContacts(url));
}

我这样调用这两个函数:

<select className="input-select select-contacts-nav"
        onChange={(e)=>{
                 this.props.onChange(e);
                 this.props.searchContacts(null, this.props);
        }}
        value={this.props.allContacts.limit}
        id="limit"
        name="ALL_CONTACTS"
>
    <option value="30"> 30</option>
    <option value="60"> 60</option>
    <option value="100">100</option>
</select>

但是this.props.searchContactsthis.props.onChange之前开始

【问题讨论】:

  • 你能说明你想打电话给fetch吗?

标签: javascript reactjs redux


【解决方案1】:

我的问题出在onChange 事件中——它有“等待状态转换”。 更多信息在这里 - React form onChange->setState one step behind

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 2021-05-26
    • 2018-11-26
    • 1970-01-01
    • 2019-10-16
    • 2020-04-23
    • 2023-03-09
    相关资源
    最近更新 更多