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
importuseSWRMutationfrom'swr/mutation'functionProfile(){const{ trigger }=useSWRMutation('/api/user',updateUserName)return(<div><h1>My name is {data.name}.</h1><buttononClick={async()=>{constnewName=data.name.toUpperCase()trigger(newName,{optimisticData: user=>({ ...user,name: newName}),rollbackOnError: true})}}>Uppercase my name!</button></div>)}
The data variable is used but it's unclear where it comes from. Initially, I expected data to be returned from the useSWRMutation hook, similar to the usage in useSWR:
const{ trigger, data }=useSWRMutation('/api/user',updateUserName)
However, this approach does not work as data in this case does not reflect the optimistic value. Could the documentation be updated to explicitly show how the optimistic data should be accessed?
The text was updated successfully, but these errors were encountered:
While implementing optimistic updates using useSWRMutation from the SWR library, I encountered an issue with the documentation available at https://swr.vercel.app/docs/mutation#optimistic-updates.
In the example provided:
The
data
variable is used but it's unclear where it comes from. Initially, I expected data to be returned from the useSWRMutation hook, similar to the usage in useSWR:However, this approach does not work as
data
in this case does not reflect the optimistic value. Could the documentation be updated to explicitly show how the optimistic data should be accessed?The text was updated successfully, but these errors were encountered: