我在返回语句中使用名为 useGetCompanyByItemId 的 useHook。
所以我收到错误“无法在回调函数中调用react-hooks”
我想做什么?
我正在查询ownitems 和shareditems。
我显示这两个项目。在 Content div 中我做映射,在那里我调用 useGetCompanyByItemId 钩子,我得到错误。
下面是我的代码,
function Parent() {
const ownedItems = [{ //somearray of objects}];
const sharedItems = [{//somearray of objects}];
const getCurrentItems = () => {
return ownedItems.concat(sharedItems);
}
return (
<Wrapper>
{getCurrentItems.length> 0 &&
<FirstWrapper>
//somedivs
</FirstWrapper>
<Content>
{springProps.map((index) => {
const item = getCurrentItems()[index];
const isSharedItem = item && item.cognitoId !== cognitoId;
const company = useGetCompanyByItemId(item.id); //here is the error
return (
<>
{isSharedItem &&
<div>
<span>company</span>
</div>
}
</>
}
)
}
);
</Content>
</Wrapper>
);
}
我不知道如何解决这个问题。我需要为 useGetCompanyById 钩子传递 item.id,但我不知道如何从 return 语句外部传递 item.id,因为这将修复该错误。
有人可以帮我解决这个错误。谢谢。