简体   繁体   English

如何在React Native中按ID显示来自API的数据

[英]how to display data from api by id in react native

I have this simple react native demo and i want to display data by specific id no all the data how can i do this ?? 我有这个简单的反应本机演示,我想按特定的ID显示数据,而没有所有的数据,我该怎么做?

      componentDidMount(){
    return fetch('http://209.97.188.74/api/scategories')
      .then((response) => response.json())
      .then((responseJson) => {
        this.setState({
          isLoading: false,
          dataSource: responseJson.data,
        }, function(){
            });
          })
         }
      render(){
          return(
      <View>
        <FlatList
          data={this.state.dataSource}
          renderItem={({item}) => <Text>{item.id}, {item.name}</Text>}
          keyExtractor={({id}, index) => id}
        />
      </View>
    );
  }

Try putting your fetch() inside of a setTimeout() 尝试将fetch()放在setTimeout()

Like this: 像这样:

 setTimeout(() => { fetch('http://209.97.188.74/api/scategories') .then((response) => response.json()) .then((responseJson) => { this.setState({ isLoading: false, dataSource: responseJson.data, }); }); }, 100); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM