繁体   English   中英

无法访问道具中的箭头 function 中的参数(React Native)

[英]Parameter within an arrow function within a prop is not accessible (React Native)

我正在尝试将参数(项目,它是 FlatList 中的数据项)传递给道具中的箭头 function。 Popover 是一个 react-native-ui-kitten 元素。 我的代码如下:

    function PostRenderItem({ item }){
        const [deleting, setDelete] = useState(false);
        //item is accessible at this point
        return(
                //item is accessible at this point
                <Popover
                visible={deleting}
                anchor={(item) => {
                    return(
                        <Text>{item.content}</Text>
                        //item undefined at this point
                    )
                }}>
                    <Button>Delete me!</Button>
                </Popover>
        )
    };

这里的问题是该item在箭头 function 中未定义,声明为锚道具。 这里的正确解决方案是什么?

<Popover
      visible={deleting}
      anchor={() => (
        <Text>{item.content}</Text> /** item is already declared in the upper-scope */
      )}
    />

或者

<Popover visible={deleting} anchor={() => renderContent(item)} />

const renderContent = item => <Text>{item.content}</Text>;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM