Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于ID显示特定数据_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript 基于ID显示特定数据

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

下面的代码显示了my data API中的标记列表,单击标记时,它会使用用户单击的标记的ID设置selectedMarker状态


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}