我想在项目中使用一些循环的映射功能创建组件,当单击任何项目以模式显示详细信息时,但它不是真实的,而不是通过单击。
。constructor(props){
super(props),
this.state={
isModalVisible: false,
}
};
requestData(){
return(
SuggestionData.map(function (i) {
const _this=this;
return(
<View key={i.id} style={styles.ContainerViewMSg3}>
<ListItem noBorder >
<View style={styles.ContainerTimer}>
<TouchableOpacity
style={styles.ReplayButton}
onPress={()=> _this._toggleModal}>
<Text>Click me!</Text>
</TouchableOpacity>
<View style={styles.ContainerOffer}>
<Text style={styles.ItemTimeMSG}>{i.OwnerMsg}:</Text>
</View>
</View>
</ListItem>
</View>
)
})
)
}
_toggleModal = () =>
this.setState({ isModalVisible: !this.state.isModalVisible });
render(){
return({this.requestData()})
}
这是我的模式的代码
<Modal isVisible={this.state.isModalVisible}>
<View style={styles.ModalView}>
<View style={styles.yellowView}>
<TouchableOpacity onPress=
{this._toggleModal}>
<IconFontAwesome name='close' size={36}
color={them.white} style=
{{ marginTop: 5, marginLeft: 210 }}/>
</TouchableOpacity>
<Image source={infoModal} style=
{styles.infoIconModal}/>
<Text style={styles.Ensure}>ok?</Text>
</View>
<View style={styles.ContentModal}>
<View style={styles.ButtonsModal}>
<TouchableOpacity style=
{styles.okButton2}><Text style=
{{ color: them.green, fontFamily: them.fontName }}>Yes</Text>
</TouchableOpacity>
<TouchableOpacity style=
{styles.cancelButton}><Text style=
{{ color: them.green, fontFamily: them.fontName }}>No</Text>
</TouchableOpacity>
</View>
</View>
</View>
</Modal>
您能帮我解决这个问题
您的代码有多个问题。 1. SuggestionData.map(function (i) {
更改此SuggestionData.map((i) => {
这将确保上下文。 2. _toggleModal = () =>
this.setState({ isModalVisible: !this.state.isModalVisible });
用_toggleModal = () => {
this.setState({ isModalVisible: !this.state.isModalVisible });}
所以应该是
requestData(){
return(
SuggestionData.map((i) => {
return(
<View key={i.id} style={styles.ContainerViewMSg3}>
<ListItem noBorder >
<View style={styles.ContainerTimer}>
<TouchableOpacity style= .
{styles.ReplayButton}
onPress={()=> this._toggleModal
} >
<Text>Click me!</Text>
</TouchableOpacity>
<View style={styles.ContainerOffer}>
<Text style={styles.ItemTimeMSG}>
{i.OwnerMsg}: </Text>
</View>
</View>
</ListItem>
</View>
)
})
)
}
_toggleModal = () => {
this.setState({ isModalVisible: !this.state.isModalVisible });
}
希望这可以解决您的问题。