Replies: 1 comment 1 reply
-
I got idea from optimistic updates and somehow managed to do it but feels little hacky. First, I created a mutation that dispatches an action to clear the state: // ...
clearAddedItems: build.mutation<{}, StreamItems>({
queryFn: () => {
// It throws error if undefined is returned. No choice but to return an empty object.
return { data: {} };
},
onQueryStarted: (arg, { dispatch }) => {
dispatch(
streamApi.util.updateQueryData('stream', arg, draft => {
draft.added = [];
}),
);
},
}),
// ...
export const { useStreamQuery, useClearAddedItemsMutation } = streamApi; And in the component: // ...
const [clearAdded, {}] = useClearAddedItemsMutation();
// ...
useEffect(() => {
if (added.length > 0) {
// Do something when item/s is/are added
// ...
// Clear added items
clearAdded(queryParam);
}
}, [added, clearAdded, queryParam]); This works but... is there better solution than this? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I'm using RTKQ + firebase(firestore) and have implemented streaming updates to subscribe and listen to real time changes from the components.
I designed my stream API's state as:
where when new item is:
added
array,modified
array, andremoved
array,and
EntityState
gets updated as well whenever above mutations occur. (added&modified: upsertMany, removed: removeMany)I designed it this way because in one component, it only consumes accumulated items returned by the query (EntityState), and in the other component, it only cares about the changed items and their cause of change (add, edit, remove).
The problem I'm facing is with the latter component.
When subscribing to the stream, I'm using selectors to split items into added, modified, and removed:
And handle them in
useEffect
:What I want to do here is to clear each state after data is consumed by the component.
In RTK days, I could simply dispatch
clearAdded
action to resetadded
property of the state to an empty array, but in RTK"Q", I can't find a way to do that.Is there any way to achieve this?
Beta Was this translation helpful? Give feedback.
All reactions