Anyone familiar with useOptimistic hook to explain how it works???
Hey everyone, im currently using the t3 stack with Next.js app router and have trying to get the useOptimistic hook alongside a server action working
So far I have managed to get this all working correctly with one caveat: I have to put "revalidatePath("<url here>")" as the very last line in my server action in order for the hook to work.
If I don't do this, the UI flashes for a brief moment and resets back to the "old state" (in my case the button flashes from filled back to unfilled)
2 Replies
Yea, you need to revalidate the data in the server action
Jack Herrington has a great video on useOptimistic - https://youtu.be/QWVr7uDyBXE
Jack Herrington
YouTube
React 19's useOptimistic: EVERYTHING you NEED to know
useOptimistic makes your pokey backend feel fast with optimistic updates to your UI. It's simple to use, but we'll covert the gotcha, as well as how to use it with React Query and also whether or not you should use it with a state manager.
Code: https://github.com/jherr/useOptimistic-video
š Upcoming NextJS course: https://pronextjs.dev
š Don'...