i am using useHook named useGetCompanyByItemId in the return statement.
and so i am getting the error "react hook cannot be called in a callback function"
<
I can see two ways of refactoring here:
Option 1: If you dont have control over the custom hook to modify
Extract the iteration into a component:
const Company = ({itemId, isSharedItem}) => {
const company = useGetCompanyByItemId(itemId);
return (<>
{isSharedItem &&
(
{company}
)
}
>);
}
Use the above component while you iterate.
Option 2: If you have control over the custom hook: I would recommend to refactor custom hook to return a method than object. Sample usage:
const {getCompanyByItemId} = useFetchCompany();
. . .
anywhere in the code,
getCompanyByItemId(itemId)
Obvious advantage with above option: