首页 > 解决方案 > 如何将带参数的函数传递给 setTimeout() 中的调度函数

问题描述

我想在收到服务器的响应后延迟调度操作。我正在使用 Redux_thunk。这是我的代码不起作用。

    export const fetchActionCreator = ({ types, url, fetchOptions, dataKey }) => dispatch => {
    const actions = typesToActionCreators(types);
    dispatch(clientRequest());
    let mock_Response = [key="A",value="xyz"];

    return axios.get(url, fetchOptions)
        .then(response => response.data,
            error => console.log('an error occoured ' + error))
        .then(data => {
            console.log('time Before ' + new Date());
            setTimeout(() => { dispatch(actions.success(mock_Response, dataKey)) }, 4000);
            console.log('time After ' + new Date());
        });
};

function typeToActionCreator(type) {
    return typeof type === 'function' ? type : (payload, dataKey) => ({ type, payload, dataKey });
}

function typesToActionCreators(types) {
    const [request, success, failure] = types.map(typeToActionCreator);
    return { request, success, failure };
}

你能告诉我我在哪里犯错了吗?

标签: reactjsreact-reduxsettimeoutaxiosredux-thunk

解决方案


推荐阅读