平面列表渲染项的 React-Native 无效钩子调用

Amo*_*pta 4 reactjs react-native react-hooks

所以我有一个 react-native flat 列表,我在每个列表中使用钩子FlatList renderItem,像这样,

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}
               />
};
Run Code Online (Sandbox Code Playgroud)

我不确定我到底做错了什么。任何相同的帮助将不胜感激。

感谢和问候。阿莫尔

Viv*_*shi 8

您正在使用RenderEntityasfunction而不是 a functional component

改变这个

renderItem={RenderEntity({ getEntityName })}
Run Code Online (Sandbox Code Playgroud)

和 :

renderItem={({item, index, separators}) => <RenderEntity item={item} getEntityName={getEntityName}/> }
Run Code Online (Sandbox Code Playgroud)