useOptimistic for Optimistic UI in Next.js Server Actions (+ Revalidate)

Поділитися
Вставка
  • Опубліковано 9 січ 2025

КОМЕНТАРІ • 26

  • @vickykumargupta605
    @vickykumargupta605 Рік тому +5

    Topics like this should be brought to the attention, this was super helpful.
    I would really appreciate if you regularly bring such topics which would optimize the UI and improve the experience.
    One day, you'll reach the height of millions subscribers.
    Good luck.

  • @loveboat
    @loveboat 11 місяців тому +8

    "Future of UI", aka the past. This was a lot simpler and very standard before serverless and server components you know.

  • @dawid_dahl
    @dawid_dahl Рік тому +2

    Will use this at work for sure. Mind blowingly cool! My clients will be happy.

    • @codiumyt
      @codiumyt 4 місяці тому

      You use unstable experimental canary features at work?

    • @dawid_dahl
      @dawid_dahl 4 місяці тому

      @@codiumyt I have a high enough degree of certainty this is going to be implemented, so it is fine!

  • @novailoveyou
    @novailoveyou Рік тому +1

    Awesome, man web development is going to get even more fun

  • @collinsk8754
    @collinsk8754 8 місяців тому +1

    Great tutorial 👍👍!

  • @ayoubahabchane
    @ayoubahabchane Рік тому

    Have you tried this on a toggle button ? Such as when the user marks a todo item as complete / incomplete. I'm having a tough time getting it to work properly; it would work fine when I toggle the first time, but if I click again before the re-validation, the optimistic update won't come through.
    Your videos are a Godsend.

  • @Edgar-pu1lc
    @Edgar-pu1lc Рік тому

    But if submitting is success,component rerender twice, yes ? When changed hook, and api after seconds, sorry for my english :)

  • @pouyajabbarisani
    @pouyajabbarisani Рік тому

    I'm trying to write test for useOptimistic using jest, but it says "TypeError: (0 , react_1.experimental_useOptimistic) is not a function" :(

  • @jonathangamble
    @jonathangamble Рік тому +1

    RevalidatePath will over fetch your database though

  • @lukq90
    @lukq90 7 місяців тому

    In my opinion it is better to use React Query + actions. We can share logic on multiple pages and revalidation is easier.

  • @twisterrjl
    @twisterrjl 6 місяців тому

    Thank you for making the docs in video format. I can't read and I can't see. I usually listen to code and instead of writing I sing it

  • @Shubham-yc6nz
    @Shubham-yc6nz Рік тому

    How to use this when we have toggle scenario. My like works find but when I unlike It does the same delay? as I can only push the values and not remove it

    • @Shubham-yc6nz
      @Shubham-yc6nz Рік тому

      I did it anyways.

    • @N8X4TE
      @N8X4TE Рік тому

      @@Shubham-yc6nz how did u do it?

  • @jorden123
    @jorden123 7 місяців тому

    But in my case, I'm always generating the ID in the server, it won't work for such a case, it will make me to create the ID in the client, and that's something that I don't really a fan of. I believe that the server must have the responsibility to work on its own.
    And if I will create a different ID than the one that in the DB and will try to do an action to that specific record, the server won't find it in the DB. Unless I will revalidate the data from the server everytime.

  • @christianstamati
    @christianstamati Рік тому

    how can I implement removeOptimisticTodo?

    • @danielk6333
      @danielk6333 6 місяців тому +1

      I know it's 7 months after this comment, but it would probably look something like this:
      const [optimisticTodos, removeOptimisticTodo] = useOptimistic(
      todos,
      (state, todoId: string) => state.filter((todo) => todo.id !== todoId)
      );
      and you would pass todo's ID to the function
      if you want to have both add and remove with one hook, you would have to implement your own reducer, or... just use useState() and either filter out the todo you want to remove or append the one you want to add in the setter

    • @christianstamati
      @christianstamati 6 місяців тому

      @@danielk6333 Thanks!

  • @ozoindaprince
    @ozoindaprince Місяць тому

    nextjs hook?

  • @lsaavedr
    @lsaavedr 6 місяців тому +1

    thanks!!!

  • @Balance-8
    @Balance-8 Рік тому

    How do you opt into this?

    • @ByteGrad
      @ByteGrad  Рік тому

      Just create a new project with npx create-next-app@latest

  • @hbela1000
    @hbela1000 Рік тому +1

    cool,thx.

  • @thebocksters2756
    @thebocksters2756 4 місяці тому

    nb