'Debounce in Redux-Toolkit's createSlice()
I am using redux-toolkit's function createSlice() to easily create reducers such as:
createSlice({
...,
reducers: {
action1(state, action) {
// ...
}
}
})
At the end of each action, I want to store the current state (for now to local storage, later into a file via electron's IPC). I do not want to write a file each time an action is called, therefore I want to debounce that call.
const save = _.debounce((state) => {/* ... */}, 500);
createSlice({
...,
reducers: {
action1(state, action) {
// ...
save(state);
}
}
})
The problem is that debouncing creates an async operation and by then, the immer proxy (into which the state is automatically wrapped by createSlice()) has already been revoked, leading to the error message illegal operation attempted on a revoked proxy.
Is there a good way to circumvent this problem? So far, I could only think of using immer's current function, but that seems somewhat ugly. I would prefer a solution where the object that was actually produced by the action is passed to the debounced function.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
