node.js - 错误:无效的挂钩调用。钩子只能在函数组件的主体内部调用。【我还在用函数组件】
问题描述
我正在关注有关构建 Shopify 应用程序的教程,但出现错误:
无效的挂钩调用。
错误发生在useMutation
const Index = () => {
const [updateProduct] = useMutation(PRODUCT_UPDATER);
const submitHandler = useCallback(() => {
let count = 0;
const runMutation = (product) => {
updateProduct({
variables: {
input: {
descriptionHtml: `${product.descriptionHtml}${descriptionValue}`,
title: `${product.title}${titleValue}`,
id: product.id,
},
},
}).then((data) => {
console.log('Update Product', count, data);
count++;
if (products[count]) runMutation(products[count]);
else {
console.log('Update Complete');
}
});
};
runMutation(products[count]);
}, [products, descriptionValue, titleValue]);
return (
<Page>
<Card>
<TextField label="Title" value={titleValue} onChange={setTitleValue} />
<TextField label="Description" value={descriptionValue} onChange={setDescriptionValue} />
<ResourcePicker
resourceType="Product"
showVariants={false}
open={pickerOpen}
onSelection={(resource) => {
console.log(resource);
setProducts(resource.selection);
}}
/>
<DataTable
columnContentTypes={['text', 'text', 'text', 'text', 'text']}
headings={['ID', 'Old title', 'New title', 'Old description', 'New description']}
rows={productTableDisplayData}
/>
<Button primary disabled={!productTableDisplayData.length} onClick={submitHandler}>
Submit
</Button>
</Card>
</Page>
);
};
export default Index;
我没有发布graphql
突变,因为我认为这只是一个钩子问题。
解决方案
你应该在 useCallback 之后返回一些 jsx
如果这是一个钩子,则将其重命名为 useIndex,或名称前带有 use 的任何内容
推荐阅读
- excel - 如何在 Excel 中使用 COUNTIF 进行条件格式设置
- android - 如何从作为上下文传递给它的类中访问片段方法
- r - 默认情况下如何为 R 中的用户定义函数分配参数?
- node.js - 在单台机器上运行多个 docker 容器的性能
- c# - Checkmarx 扫描中的不安全对象绑定
- amazon-web-services - 如何通过在多个 AWS 实例上运行 Jmeter 测试来对 10 万唯一用户的系统进行负载测试?
- javascript - youtube 视频的自定义 JS,需要声音才能与自动播放一起使用
- python - Pyinstaller - 在 dist 中包含空文件夹
- java - 易失性写入是否也会刷新非易失性写入?
- java - 如何在流批处理流连接中定义连接条件?