DEV Community

Anjan Talatam
Anjan Talatam

Posted on

Can we use useQuery for POST request

Yes

use useQuery() if you are fetching data from the server. ( irrespective of methods GET/ POST )

use useMutation() if you are modifying data on the server.


Back story 💭

I am under a misconception, that useQuery is only for GET requests and all other requests ( POST, PUT, DELETE ) are handled by useMutation.

But as mentioned in the above tweet I got the below case.

Need to fetch data using a POST request.

Initially, I invoked useMutation on the mount with useEffect. ( To get the Data )

and never used thereafter in the code to mutate any data.

This looked like a useQuery material!

Upon searching Can we use useQuery for POST requests, I didn't find a direct blog/ article but on reading through the DOCS found that we can use useQuery for POST requests as well and got a much neater understanding on what useQuery and useMutation are meant for.

If you got a similar use case go ahead and use useQuery ✅

Top comments (0)