[英]Pass dynamic value to react native Modal View from flatList Item
I have a FlatList like... <FlatList data={data} keyExtractor={(item, index) => index.toString()}renderItem={({item,index}) =>(<NewView id={item.id} index={index}/>) }/>
Now for render Item const NewView = ({id, index}) =>{return(<TouchablOpacity onPress={()=> openModal()}> <Text>{id}</Text></TouchableOpacity><Modal animationType='fade' transparent={true}visible={userOptionShowModal}onRequestClose={() =>setUserOptionShowModal(false)}}><Text>{?? ==>id}</Text></Modal>); }
我有一个像...
<FlatList data={data} keyExtractor={(item, index) => index.toString()}renderItem={({item,index}) =>(<NewView id={item.id} index={index}/>) }/>
现在渲染 Item const NewView = ({id, index}) =>{return(<TouchablOpacity onPress={()=> openModal()}> <Text>{id}</Text></TouchableOpacity><Modal animationType='fade' transparent={true}visible={userOptionShowModal}onRequestClose={() =>setUserOptionShowModal(false)}}><Text>{?? ==>id}</Text></Modal>); }
const NewView = ({id, index}) =>{return(<TouchablOpacity onPress={()=> openModal()}> <Text>{id}</Text></TouchableOpacity><Modal animationType='fade' transparent={true}visible={userOptionShowModal}onRequestClose={() =>setUserOptionShowModal(false)}}><Text>{?? ==>id}</Text></Modal>); }
Now when i want to show modal on Pressing each flatlist item, its working but when Pressing one flatlist item showing the modal but in the modal item i cannot show the index or id or any dynamic value passed (how?) of that particular flatListItem... Please help how to achieve it?现在,当我想在按下每个
const NewView = ({id, index}) =>{return(<TouchablOpacity onPress={()=> openModal()}> <Text>{id}</Text></TouchableOpacity><Modal animationType='fade' transparent={true}visible={userOptionShowModal}onRequestClose={() =>setUserOptionShowModal(false)}}><Text>{?? ==>id}</Text></Modal>); }
项目时显示模态,它的工作但是当按下一个 flatlist 项目显示模态但在模态项目中我无法显示索引或 id 或任何传递的动态值(如何?)那个特定的 flatListItem ...请帮助如何实现它?
You can achieve this behavior in many ways.您可以通过多种方式实现此行为。 One of the easiest way is to create a state that holds this information, then on modal close, you reset its value.
最简单的方法之一是创建一个 state 来保存此信息,然后在模式关闭时重置其值。
First, add this useState:首先,添加这个 useState:
const [passedData, setPassedData] = useState({});
Then when you call the onPress, add all the info you need:然后,当您调用 onPress 时,添加您需要的所有信息:
<TouchablOpacity onPress={()=> openModal({id, index})}>...</TouchablOpacity>
PS: openModal({id, index}) is same as openModal({id: id, index: index}) PS: openModal({id, index}) 等同于 openModal({id: id, index: index})
Then in your openModal function, add this:然后在你的 openModal function 中添加:
const openModal = ({id, index}) => {
...
setPassedData({id, index})
}
Then you can access it in your modal (Don't forget to reset the passed data onCloseRequest or anytime you close the modal):然后你可以在你的模态中访问它(不要忘记重置传递的数据 onCloseRequest 或任何时候你关闭模态):
<Modal
animationType="fade"
transparent={true}
visible={userOptionShowModal}
onRequestClose={() =>{
setUserOptionShowModal(false)
setPassedData({})
}}>
<Text>id : {passedData.id}, index: {passedData.index}</Text>
</Modal>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.