【发布时间】:2020-12-01 09:02:57
【问题描述】:
我在 React Native 中遇到了这个错误:
"Invariant Violation: 无效的钩子调用。钩子只能在函数组件的主体内部调用。等等"
我的代码(简化)是这样的:
import { useApolloClient } from '@apollo/react-hooks';
// code
const handleRedirectCalculator = React.useCallback(() => {
const client = useApolloClient();
client.clearStore();
navigation.push('CurrencyConverter');
}, []);
return (
<View>
<MessageErrorButton
message='El tiempo de tu cotización a caducado'
onPress={() => handleRedirectCalculator()}
/>
我想要的是使用 useApollo 挂钩来获取客户端,然后在客户端上使用 clearStore。如何修复我的代码?
【问题讨论】:
-
我认为你可以在 useCallback 之外定义
const client = useApolloClient();
标签: reactjs react-hooks