Код: Выделить всё
const initialState = {
dbItems: [],
items: [],
};
< /code>
и в асинхронизации я делаю что -то вроде этого: < /p>
export const updateItem = createAsyncThunk(`${ REDUCERS.TASKS }/updateItem`, async (
data,
{
dispatch,
getState,
rejectWithValue,
requestId
}
) => {
// Optimistic update, this is where I mutate the items state, nothing fancy happening in this reducer
dispatch(optimisticUpdate(data));
try {
const updatedTask = await myRequest(data);
return updatedTask;
} catch (error) {
const { dbItems } = getState().items;
// Not sure if I actually need requestId here since it's not stored somewhere.
const itemId = data.id ?? requestId;
// Revert to previous task if db update fails
const originalItem = dbItems.find(({ id }) => id === itemId);
return rejectWithValue(error, { originalItem });
}
});
Код: Выделить всё
.addCase(updateItem.rejected, (state, { meta }) => {
// Not sure if I actually need requestId here either.
const itemId = meta.arg.id ?? meta.requestId;
const itemIndex = state.tasks.findIndex(({ id }) => id === itemId);
if (itemIndex < 0) {
return;
}
const { originalItem } = meta;
if (originalItem) {
state.tasks[ itemIndex ] = originalItem;
} else {
state.tasks.splice(itemIndex, 1);
}
})
< /code>
Проблема в том, что я должен хранить два отдельных состояния (dbItemsПодробнее здесь: https://stackoverflow.com/questions/794 ... ux-toolkit
Мобильная версия