-
Notifications
You must be signed in to change notification settings - Fork 104
Closed
Description
Right now with useOptimisticMutation
you write logic like:
import { useOptimisticMutation } from '@tanstack/react-db'
const Todo = () => {
// Create the `addTodo` mutator, passing in your `mutationFn`.
const addTodo = useOptimisticMutation({ mutationFn })
const handleClick = () => {
// Triggers the mutationFn
addTodo.mutate(() =>
// Instantly applies the local optimistic state.
todoCollection.insert({
id: uuid(),
text: '🔥 Make app faster',
completed: false
})
)
}
return <Button onClick={ handleClick } />
}
The problem with this though is it splits logic between the useOptimisticMutation
hook & the .mutate call site.
If we add a onMutate
hook then you could write:
import { useOptimisticMutation } from '@tanstack/react-db'
const Todo = () => {
// Create the `addTodo` mutator, passing in your `mutationFn`.
const addTodo = useOptimisticMutation({
mutationFn,
onMutate: (text) => {
// Instantly applies the local optimistic state.
todoCollection.insert({
id: uuid(),
text,
completed: false
})
}
})
const handleClick = () => {
// Triggers the `onMutate`
addTodo.mutate('🔥 Make app faster')
}
return <Button onClick={ handleClick } />
}
Metadata
Metadata
Assignees
Labels
No labels