检查范围之外的变量
Checking variable outside of its scope
在我的代码中,我想有条件地执行查询,然后在模板中检查其加载变量,但前提是执行了查询并且不想在我的文件中出现任何 TypeScript 错误。如何检查此变量在其上下文中可能存在或可能不存在?
我正在使用 Apollo GraphQL。
if( executeMyQuery )
{
const {
data, loading: loadingQuery
} = useQuery( MyQuery );
}
return (
<View>
{ /* ... some code ... */ }
{ executeMyQuery && loadingQuery ?
<Text>Query is loading...</Text>
{ /* ... some code ... */ }
: null }
</View>
);
打字稿说:Cannot find name 'loadingQuery'.
const {
data, loading: loadingQuery
} = useQuery( MyQuery, {
// The query will not execute until the userId exists
enabled: executeMyQuery,
}
);
return (
<View>
{ /* ... some code ... */ }
{ executeMyQuery && loadingQuery ?
<Text>Query is loading...</Text>
{ /* ... some code ... */ }
: null }
</View>
);
通过更改
使用 skip
参数修复了它
if( executeMyQuery )
{
const {
data, loading: loadingQuery
} = useQuery( MyQuery );
}
到
const {
data, loading: loadingQuery
} = useQuery( MyQuery, {
skip: executeMyQuery,
} );
在我的代码中,我想有条件地执行查询,然后在模板中检查其加载变量,但前提是执行了查询并且不想在我的文件中出现任何 TypeScript 错误。如何检查此变量在其上下文中可能存在或可能不存在?
我正在使用 Apollo GraphQL。
if( executeMyQuery )
{
const {
data, loading: loadingQuery
} = useQuery( MyQuery );
}
return (
<View>
{ /* ... some code ... */ }
{ executeMyQuery && loadingQuery ?
<Text>Query is loading...</Text>
{ /* ... some code ... */ }
: null }
</View>
);
打字稿说:Cannot find name 'loadingQuery'.
const {
data, loading: loadingQuery
} = useQuery( MyQuery, {
// The query will not execute until the userId exists
enabled: executeMyQuery,
}
);
return (
<View>
{ /* ... some code ... */ }
{ executeMyQuery && loadingQuery ?
<Text>Query is loading...</Text>
{ /* ... some code ... */ }
: null }
</View>
);
通过更改
使用skip
参数修复了它
if( executeMyQuery )
{
const {
data, loading: loadingQuery
} = useQuery( MyQuery );
}
到
const {
data, loading: loadingQuery
} = useQuery( MyQuery, {
skip: executeMyQuery,
} );