0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

useQuery で取得した変数を 別の useQuery の variables に代入する方法

Posted at

備忘録。

下のコードのようなことをしようとしても、怒られたり、動かなかったりする。

const {data: data1, loading: loading1, error: error1} = useQuery(SOME_QUERY1)
const {data: data2, loading: loading2, error: error2} = useQuery(SOME_QUERY2, {
  variables: {hoge: data1.someQuery1.hoge}
})

そのような場合は、useLazyQuery と useEffect を使う。

const {data: data1, loading: loading1, error: error1} = useQuery(SOME_QUERY1)
const [fetchSecondData , {data: data2, loading: loading2, error: error2}] = useLazyQuery(SOME_QUERY2)

useEffect(() => {
  if (data1.someQuery1.hoge) {
    fetchSecondData({variables: {hoge: data1.someQuery1.hoge}})
  }
}, [data1])

if (loading1 || loading2) return <div>loading</div>
if (error1 || error2) return <div>error</div>

return(
  <>data2.someQuery2.hoge</>
)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?