[英]Parameter within an arrow function within a prop is not accessible (React Native)
I'm trying to pass a parameter (item, which is a data item within a FlatList) to an arrow function within a prop.我正在尝试将参数(项目,它是 FlatList 中的数据项)传递给道具中的箭头 function。 Popover is a react-native-ui-kitten element.
Popover 是一个 react-native-ui-kitten 元素。 My code is given below:
我的代码如下:
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>
)
};
The issue here is that item
is undefined within the arrow function declared as the anchor prop.这里的问题是该
item
在箭头 function 中未定义,声明为锚道具。 What is the proper solution here?这里的正确解决方案是什么?
<Popover
visible={deleting}
anchor={() => (
<Text>{item.content}</Text> /** item is already declared in the upper-scope */
)}
/>
OR或者
<Popover visible={deleting} anchor={() => renderContent(item)} />
const renderContent = item => <Text>{item.content}</Text>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.