【发布时间】:2021-01-31 19:22:58
【问题描述】:
我有一个组件,我在其中调用我的自定义钩子。
自定义挂钩如下所示:
import { useQuery } from 'react-apollo';
export function useSubscription() {
const { loading, error, data } = useQuery(GET_SUBSCRIPTION_BY_ID)
if (loading) return false
if (error) return null
return data
}
然后我正在使用它导致错误的组件是:
export default function Form(props) {
const router = useRouter();
let theSub = useSubscription();
if (theSub === false) {
return (
<Spinner />
)
} // else I'll have the data after this point so can use it.
useEffect(() => {
if (!isDeleted && Object.keys(router.query).length !== 0 && router.query.constructor === Object) {
setNewForm(false);
const fetchData = async () => {
// Axios to fetch data
};
fetchData();
}
}, [router.query]);
// Form Base States
const [newForm, setNewForm] = useState(true);
const [activeToast, setActiveToast] = useState(false);
// Form Change Methods
const handleUrlChange = useCallback((value) => setUrl(value), []);
const handleSubmit = useCallback(async (_event) => {
// Submit Form Code
}, [url, selectedDuration, included, excluded]);
return (
<Frame>
My FORM
</Frame>
)
}
有什么想法吗?
【问题讨论】:
-
你是否有条件地调用任何钩子?
-
不 - 没有条件
-
可以分享
Form组件的完整代码吗? -
@Yousaf - 我已经用表单组件代码更新了帖子
-
尝试将
if (theSub === false) {移动到return (...)之前。
标签: reactjs react-hooks