isOptimistic turns false before data are returned from server.
I have switch which toggle value using useServerAction.
Everything works great but when using optimistic update the data becomes undefined for moment when switching from optimistic value to server returned value. Resulting in bad flicker of the switch.
In my client component I have folowing console log:
console.log("data: ", data, "isOptimistic: ", isOptimistic, "status:", status);
This prints when I flip the switch:
data: true isOptimistic: false status: success
data: true isOptimistic: false status: success
data: true isOptimistic: true status: pending
data: true isOptimistic: true status: pending
This prints after server action request returns:
data: undefined isOptimistic: false: status pending
data: undefined isOptimistic: false: status pending
data: true isOptimistic: false status: success
data: true isOptimistic: false status: success
data: undefined, isOptimistic: false is the problem because switch doesnt know what value to show.
It is fixed immedialy with: data: true, but quick flicker is anoying.
I believe data should get the optimistic value until isOptimistic is false.
0 条评论