DEV Community

Anjan Talatam
Anjan Talatam

Posted on

1

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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay