如何将2个graphql查询与react-apollo-hooks结合使用,其中第二个查询依赖于从第一个查询中检索到的参数?
我尝试使用2个查询,如下所示:
const [o, setO] = useState()
const { loading: loadingO, error: errorO, data: dataO } = useQuery(Q_GET_O, { onCompleted: d => setO(d.getO[0].id) });
if (loadingO) { return "error" }
const { loading: loadingOP, error: errorOP, data: dataOP } = useQuery(Q_GET_OP, { variables: { o } })但是,当我运行我的项目时,react-hooks会给我以下消息:
"index.js:1437警告: React检测到升级调用的挂钩的顺序发生了变化。如果不进行修复,这将导致错误和错误。有关详细信息,请阅读挂钩规则“
我想知道如何使用react-apollo-hooks来运行依赖于另一个查询的查询。如果事先知道graphql查询变量,那么它非常有用。但是,对于来自其他查询变量,我没有找到解决方案。
发布于 2019-08-20 02:05:47
您可以将skip选项添加到第二个查询中,但会丢失if条件:
const { loading: loadingOP, error: errorOP, data: dataOP }
= useQuery(Q_GET_OP, { variables: { o }, skip: !o })来自文档:If skip is true, the query will be skipped entirely
发布于 2019-10-11 22:34:28
这里的问题是,在所有钩子都有机会运行之前,您正在短路返回。
如果你在调用所有钩子之前退出渲染函数,React将会报错。
例如:
function BrokenFoo () {
const query = useSomeQuery();
if (query.loading) return <Loading />
// This will cause some issues because
// it's possible that we return before our useState hook gets called
const [bar, setBar] = useState();
return <SomeComponent bar={bar} setBar={setBar} data={query.data} />
}要修复,请执行以下操作:
function FixedFoo () {
// This will be fine because
// all of the hooks have a chance to be called before a return
const query = useSomeQuery();
const [bar, setBar] = useState();
if (query.loading) return <Loading />
return <SomeComponent bar={bar} setBar={setBar} data={query.data} />
}https://stackoverflow.com/questions/57560352
复制相似问题