Dob*_*obr 6 reactjs redux redux-thunk react-redux redux-toolkit
我正在从 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);
Run Code Online (Sandbox Code Playgroud)
问题是当我转到 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)
Run Code Online (Sandbox Code Playgroud)
我该如何让它发挥作用?
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
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4564 次 |
| 最近记录: |