pet*_*gan 32 javascript ecmascript-6 reactjs react-apollo
我一直在使用react-apollo
和渲染道具方法。
这工作得很好,我的代码看起来像这样。
const App = () => {
const [player, setPlayer] = React.useState(null);
if (player) {
return (
<GetBroncosPlayerQuery variables={{ player }}>
{({ data }) => {
// do stuff here with a select box
}}
</GetBroncosPlayersQuery>
)
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试对useQuery
钩子做同样的事情,当我的代码如下所示时,我会收到以下错误:
const App = () => {
const [player, setPlayer] = React.useState(false);
if (isBroncos) {
const { data } = useQuery(GetBroncosPlayersQueryDocument, {
variables: { player }
});
// do stuff here
}
}
Run Code Online (Sandbox Code Playgroud)
这给出了一个错误,你不能在条件语句中使用钩子。然后我实现了useLazyQuery
但是这也不起作用,因为一旦它表现得像useQuery
,它就会起作用,所以它第一次起作用,但是如果用户将选择下拉列表再次更改为空,它会中断。
查询挂钩仅有条件地调用查询的最佳方法是什么?
Den*_*ash 68
You should use skip
option:
If skip is true, the query will be skipped entirely.
const isBroncos = getCondition();
const App = () => {
const [player, setPlayer] = React.useState(false);
const { data } = useQuery(GetBroncosPlayersQueryDocument, {
variables: { player },
skip: isBroncos
});
return !isBroncos && data && <>...</>;
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9527 次 |
最近记录: |