Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document optimisticData and rollbackData #20

Open
garrettg123 opened this issue Jan 6, 2024 · 1 comment
Open

Document optimisticData and rollbackData #20

garrettg123 opened this issue Jan 6, 2024 · 1 comment

Comments

@garrettg123
Copy link

I just found out that you can do this for optimistic updates. This is amazing and should be documented:

const completeTaskMutationResult = api.task.complete.useMutation({
  onMutate(variables) {
    const updatedTask = {
      ...task,
      completedDate: variables.isCompleted ? new Date() : null,
    } satisfies Task

    return {
      optimisticData: updatedTask,
      rollbackData: task,
    }
  }
})
@klis87
Copy link
Owner

klis87 commented Jan 6, 2024

Did you see this - https://github.com/klis87/normy/tree/master/packages/normy-react-query#optimistic-updates-arrow_up ?

By the way, you could even do something like this:

const completeTaskMutationResult = api.task.complete.useMutation({
  onMutate(variables) {
    const updatedTask = {
      id: task.id,
      completedDate: variables.isCompleted ? new Date() : null,
    } 

    return {
      optimisticData: updatedTask,
      rollbackData: task,
    }
  }
})

, because you need to pass only attributes which are changed (plus identifier you use for objects). Your way works as well, just curiosity.

You might be also interested in #11 - rollbackData could be calculated automatically based on optimisticData, as all it really should be is a denormalized object based on the same structure as optimisticData object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants