I'm learning react query and i'm trying to post data with it.
I'm able to fetch data with react-query, but I can't do the same posting data. My question is if it's possible to post data with react-query and how to do it.
Thanks :)
Here is a quick example of React Query mutation
const mutation = useMutation({
mutationFn: (newTodo) => {
return axios.post('/todos', newTodo)
},
})
React Query - Mutations Docs
Please read react-query docs clearly, it provides all things about the library.
To fetch (GET) data, you can use:
useQuery(): For 1 query at a timeuseQueries(): For parallel queries at a timeuseInfiniteQuery(): In case of "infinite scroll"To create/update/delete (POST/PUT/PATCH/DELETE) data, you can use useMutation(). useMutation() returns an object that provides 2 options for you to mutate data:
mutate(): To trigger the mutation and optionally hooks on additional callback (onSuccess, onSettled, onError)mutateAsync(): Similar to mutate() but returns a promise which can be awaitedIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With