gpt4 book ai didi

reactjs - 如何从 Redux Toolkit 中消除 createAsyncThunk

转载 作者:行者123 更新时间:2023-12-04 14:06:29 28 4
gpt4 key购买 nike

我正在从 Redux 迁移到 Redux Toolkit。我这里的简化代码用于使用 lodash/debounce 进行去抖动更新。

import debounce from "lodash/debounce";

const updateApplication = async (app, dispatch) => {
const state = getState();

try {
const result = await update(app);
dispatch({
type: UPDATE,
result: result
});
} catch (err) {
console.log(err);
}
};

export default debounce(updateThunk, 2000);
问题是当我转到 createAsyncThunk 时,它不会被执行。

const updateApp = createAction("app/update");
const updateApplication = createAsyncThunk(
"app/updateDebounced",
async (app, { dispatch }) => {

try {
const result = await update(app);
dispatch(updateApp(result))
);
}
} catch (err) {
// console.log(err);
}
}
);

export default debounce(updateApplication, 2000)
我如何使它工作?

最佳答案

const updateApp = createAction("app/update");
const handler = async (app, { dispatch }) => {
try {
const result = await update(app);
dispatch(updateApp(result))
);
}
} catch (err) {
// console.log(err);
}
}
const debouncedHandler = debounce(handler, 2000);

export default createAsyncThunk(
"app/updateDebounced",
debouncedHandler
);

关于reactjs - 如何从 Redux Toolkit 中消除 createAsyncThunk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68281810/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com