Javascript 基于ID显示特定数据
下面的代码显示了my data API中的标记列表,单击标记时,它会使用用户单击的标记的ID设置selectedMarker状态Javascript 基于ID显示特定数据,javascript,reactjs,react-native,Javascript,Reactjs,React Native,下面的代码显示了my data API中的标记列表,单击标记时,它会使用用户单击的标记的ID设置selectedMarker状态 getPosts() { axios .get("mydataAPI") .then(response => { this.setState({ posts: response.data.response }); }); } ///This code is t
getPosts() {
axios
.get("mydataAPI")
.then(response => {
this.setState({
posts: response.data.response
});
});
}
///This code is then returned to the user with MapView to display a list of markers on map, when a user clicks a marker the ID of that marker is stored in state ///
{this.state.posts.map((user, index)=> {
return (
<MapView.Marker key={user.ID} coordinate={{latitude: user.lat, longitude: user.lng}} onPress={() => {this.setState({selectedMarker: user.ID})}} title={user.category} description={Moment(user.post_date).fromNow(), user.category, user.address} image={require('../images/yes.png')}/>
);
})}
//// will display ID of marker user selected ////
{ this.state.selectedMarker }
既然已将标记ID保存为状态,如何使用.map函数根据其ID显示有关特定标记的信息?您可以使用lodash npm软件包 在{showUserDetailsuser.ID}/>上添加函数 然后在渲染方法中:
{this.state.Index !== -1 ? (
<Text>this.state.posts[this.state.Index].ID</Text>
<Text>this.state.posts[this.state.Index].lat</Text>
<Text>this.state.posts[this.state.Index].lng</Text>
) : null}
如果不起作用,请告诉我此.state.posts中的数据
{this.state.Index !== -1 ? (
<Text>this.state.posts[this.state.Index].ID</Text>
<Text>this.state.posts[this.state.Index].lat</Text>
<Text>this.state.posts[this.state.Index].lng</Text>
) : null}