Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post with react-query

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 :)

like image 324
Josep Avatar asked Feb 08 '26 04:02

Josep


2 Answers

Here is a quick example of React Query mutation

const mutation = useMutation({
   mutationFn: (newTodo) => {
     return axios.post('/todos', newTodo)
   },
 })

React Query - Mutations Docs

like image 104
todevv Avatar answered Feb 12 '26 14:02

todevv


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 time
  • useQueries(): For parallel queries at a time
  • useInfiniteQuery(): 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 awaited
like image 35
haptn Avatar answered Feb 12 '26 14:02

haptn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!