【问题标题】:Parameter within an arrow function within a prop is not accessible (React Native)无法访问道具中箭头函数内的参数(React Native)
【发布时间】:2020-12-29 18:08:49
【问题描述】:

我正在尝试将参数(项目,它是 FlatList 中的数据项)传递给道具中的箭头函数。 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 在声明为锚属性的箭头函数中未定义。这里的正确解决方案是什么?

【问题讨论】:

  • 你不需要包含 item 作为箭头函数参数,因为上面已经有了它

标签: javascript react-native react-native-ui-kitten


【解决方案1】:
<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>;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多