【问题标题】:React-Native Invalid hook call for Flat List Render Item平面列表渲染项的 React-Native 无效挂钩调用
【发布时间】:2020-10-04 19:35:07
【问题描述】:

所以我有一个 react-native 平面列表,我在每个 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}
               />
};

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

谢谢问候。 阿莫尔

【问题讨论】:

    标签: reactjs react-native react-hooks


    【解决方案1】:

    您将RenderEntity 用作function 而不是functional component

    改变这个

    renderItem={RenderEntity({ getEntityName })}
    

    与:

    renderItem={({item, index, separators}) => <RenderEntity item={item} getEntityName={getEntityName}/> }
    

    【讨论】:

    • 嗯,我试试看。只是出于好奇,我们可以使用问题中提到的柯里化吗?
    • 首先检查getEntityName是否返回任何东西,如果返回则可以使用它
    • 那么get entityname返回一个字符串,我可以用React.createElement吗?
    • 像魅力一样工作!谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    相关资源
    最近更新 更多