should I put useQuery inside a useEffect and should I store returned data in state?
See original GitHub issueStill wrapping my head around using this library with modern react hooks patterns.
Because data fetching is considered a side effect I’ve seen this pattern a lot in tutorials and docs.
const [data, setData] = useState([]);
useEffect(() => {
fetch("http://localhost:3001/links/")
.then(response => response.json())
.then(data => setData(data));
},[]);
Should I be doing a similar pattern with this library? ie putting useQuery in useEffect and storing the data returned from useQuery inside useState
Or can I simply just use useQuery as is and directly access the data returned in my components.
Lets assume I’m just building a simple todo app where I want to create, update and delete todo items.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:64
- Comments:21
Top Results From Across the Web
How do you handle useQuery with useEffect? - Help
I need to edit a form with data coming from useQuery. My issue is that react imposes an order to execute hooks, and...
Read more >Why I avoid useEffect For API Calls and use React Query ...
If you are like me, always using use effect whenever you need to grab data from an API then wait for it to...
Read more >Setting React State after useQuery? : r/graphql - Reddit
I am unable to use the data property returned from Apollo useQuery because the input field using has two way binding.
Read more >Async data made simple with React Query - Hey! I'm Tyler
useQuery is a custom hook. Which means that it is made up of many React hooks internally. One of many hooks it uses...
Read more >Getting Started With useQuery (React Query) - Medium
import { useQuery } from "react-query";. And now we can use “useQuery” hook to manage the data fetching as below, const response =...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I don’t store return data from query to react state. I just feel like that’s an anti-pattern. You should just use the returned data as it is like below:
But if you must store the returned data, could do below:
Yeah the useQuery should be already an “effect” hook, so you don’t have to do that inside useEffect. the useEffect was meant for async stuff, like fetch, but the useQuery is not an async function, it will immediately return result (loading:true, data:null). Then re-return the result when data is ready. so it does not have to be in useEffect.