question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

should I put useQuery inside a useEffect and should I store returned data in state?

See original GitHub issue

Still 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:closed
  • Created 4 years ago
  • Reactions:64
  • Comments:21

github_iconTop GitHub Comments

194reactions
jinshin1013commented, Feb 24, 2020

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:

const { loading, data, error } = useQuery(SOME_QUERY)

// If you absolutely need to cache the mutated data you can do the below. But
// most of the time you won't need to use useMemo at all.
const cachedMutatedData = useMemo(() => {
  if (loading || error) return null

  // mutate data here
  return data
}, [loading, error, data])

if (loading) return <Loader />
if (error) return <Error />

// safe to assume data now exist and you can use data.
const mutatedData = (() => {
  // if you want to mutate the data for some reason
  return data
})()

return <YourComponent data={mutatedData}  />

But if you must store the returned data, could do below:

const { loading, data, error } = useQuery(SOME_QUERY)
const [state, setState] = React.useState([])

React.useEffect(() => {
  // do some checking here to ensure data exist
  if (data) {
    // mutate data if you need to
    setState(data)
  }
}, [data])
44reactions
hansololaicommented, May 9, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found