So i have a react-native flat list where i use hooks in each FlatList
renderItem
, something like this,
export const RenderEntityList = (props) => {
const { entityList } = props;
const getEntityName = useCallBack((entity) => {
//...get Entity name from list
}, [entityList]);
return <FlatList
data={entityList}
renderItem={RenderEntity({ getEntityName })}
/>
};
const RenderEntity = (props) => {
const { getEntityName } = props;
return (props) => {
const { item: entity } = props;
// This is where i get the error Invalid hook call;
const [entityName, setEntityName] = useState('');
useEffect(() => {
setEntityName(getEntityName(entity))
}, [entity])
return <ListItem
title={entityName}
/>
};
I am not sure what exactly i am doing wrong. Any help on the same would be highly appreciated.
Thanks Regards. Amol
If the error persists, try to delete your node_modules and package-lock. json (not package. json ) files, re-run npm install and restart your IDE. The error is often caused by having multiple versions of react in the same project.
Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3.
Mismatching Versions of React and React DOM You can run npm ls react-dom or npm ls react-native in your application folder to check which version you're using.
This could happen for one of the following reasons: You might have mismatching versions of React and the renderer (such as React DOM) You might be breaking the Rules of Hooks. You might have more than one copy of React in the same app See for tips about how to debug and fix this problem.
You are using RenderEntity
as function
instead of a functional component
:
Change this
renderItem={RenderEntity({ getEntityName })}
With :
renderItem={({item, index, separators}) => <RenderEntity item={item} getEntityName={getEntityName}/> }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With