You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi everyone, I'm using react-query with firebase, so far, so good.
But I encountered an issue when implementing optimistic updates
const[updateTodo]=useMutation(async(newTodo)=>{constref=firebase.database().ref('todos').push(newTodo)console.log(ref.key)// Here I have a todo ID before any network operationreturnawaitref},{// When mutate is called:onMutate: newTodo=>{queryCache.cancelQueries('todos')constpreviousTodos=queryCache.getQueryData('todos')queryCache.setQueryData('todos',old=>[...old,newTodo])// Issue here : I would like to access ref.keyreturn()=>queryCache.setQueryData('todos',previousTodos)},onError: (err,newTodo,rollback)=>rollback(),onSettled: ()=>{queryCache.invalidateQueries('todos')},})consthandleTodoAddClick=()=>updateTodo(pendingTodo)
I thought about a way to do it but I'm not sure about how it will integrate with react-query
const[updateTodo]=useMutation(async(newTodo)=>{constref=firebase.database().ref('todos').push(newTodo)queryCache.cancelQueries('todos')// Get previous todos in case of rollbackconstpreviousTodos=queryCache.getQueryData('todos')queryCache.setQueryData('todos',old=>[...old,{id: ref.key, ...newTodo}])try{awaitref}catch(error){// rollbackqueryCache.setQueryData('todos',previousTodos)}finally{queryCache.invalidateQueries('todos')}})
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi everyone, I'm using react-query with firebase, so far, so good.
But I encountered an issue when implementing optimistic updates
I thought about a way to do it but I'm not sure about how it will integrate with react-query
Beta Was this translation helpful? Give feedback.
All reactions