Query or mutation? #1546
-
I've got a question about React Query ideology As far as I know, queries are used to fetch data from the server. Usually you use it for GET requests. Mutations, on the contrary, are used to modify data on the server. You use those for anything but GET requests. In my case I have a POST request I use to send my data from web page to a printer. The thing is, this request doesn't modify any data on the server side, neither does it fetch any data for me. So I am not sure whether I should use mutation or query. Additionally, I'd like this request to be made on the first render of my component. Thus, if using mutation, I have to call it in a useEffect hook, which seems to be too much So what should I do? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I think useMutation in an effect seems the right choice. With useQuery, you should be ready that this fires quite often - for example, when the user loses their internet connection and then reconnects. If your query performs effects on the server, this is not a good idea. Also, you’ve said that your post request doesn’t return anything, so i assume it stores something on the printer? Or even prints a page :) |
Beta Was this translation helpful? Give feedback.
-
If it isn't fetching data or "subscribing" your app to some remote data ( Use |
Beta Was this translation helpful? Give feedback.
I think useMutation in an effect seems the right choice. With useQuery, you should be ready that this fires quite often - for example, when the user loses their internet connection and then reconnects. If your query performs effects on the server, this is not a good idea. Also, you’ve said that your post request doesn’t return anything, so i assume it stores something on the printer? Or even prints a page :)
Anyways, those are all side effects, so useMutation sounds good to me.