Mutations | TanStack Query Docs

PHOTO EMBED

Mon Nov 28 2022 13:14:14 GMT+0000 (Coordinated Universal Time)

Saved by @jacopo #tsx

// This will not work in React 16 and earlier
const CreateTodo = () => {
  const mutation = useMutation({ mutationFn: event => {
    event.preventDefault()
    return fetch('/api', new FormData(event.target))
  }})


  return <form onSubmit={mutation.mutate}>...</form>
}


// This will work
const CreateTodo = () => {
  const mutation = useMutation({ mutationFn: formData => {
    return fetch('/api', formData)
  }})
  const onSubmit = event => {
    event.preventDefault()
    mutation.mutate(new FormData(event.target))
  }


  return <form onSubmit={onSubmit}>...</form>
}
content_copyCOPY

https://tanstack.com/query/v4/docs/guides/mutations