реагировать на родной вызов API - PullRequest
0 голосов
/ 17 апреля 2020

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 Ответ

1 голос
/ 17 апреля 2020

Вы не можете вызвать такую ​​функцию, как изменение

<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>

Эта функция будет вызываться, когда мы нажимаем текст щелчка.

Надеюсь, что это помогает!

...