反应本机调用API提取

时间:2020-04-17 03:54:42

标签: api react-native fetch

enter image description here

请帮助我调用此函数。 我想从Wordpress API获取类别计数和类别名称 抱歉,谢谢您。

  CatName(item){
  fetch('http://anbaetv.ma/wp-json/wp/v2/categories/'+item.categories)
  .then((response) => response.json())
  .then((json) => {
    this.setState({
      jsonData: json.count,
    });
  })
  .catch((error) => {
    console.error(error);
  });
 }

renderItem = ({item}) => { 
  return(
    <TouchableOpacity onPress={() => this._onPress(item)}>
      <View style={{flex:1, flexDirection: 'row', marginBottom: 2,}}>
        <Image 
          style={{ width:100, height:80, margin:5}}
          source={{uri: item.better_featured_image ? 
            item.better_featured_image.media_details.sizes.thumbnail.source_url : 'https://via.placeholder.com/150/0000FF/808080?Text=NotFound'}}
        />
        <View style={{flex:1, justifyContent:"center",marginLeft:5}}>
          <Text style={{fontSize: 18, marginBottom: 15}}>{item.title.rendered}</Text>
          <Text style={{fontSize: 18, marginBottom: 15,color:"red"}}>{this.CatName(item)}</Text>
        </View>
      </View>
    </TouchableOpacity>
  );
}

1 个答案:

答案 0 :(得分:1)

您不能调用类似更改的功能

<Text style={{fontSize: 18, marginBottom: 15,color:"red"}}>{this.CatName(item)}</Text>

<Text onPress={this.CatName(item)} style={{fontSize: 18, marginBottom: 15,color:"red"}}>click</Text>

当我们点击点击文本时,它将调用该函数。

希望这会有所帮助!