0

enter image description here

Help me to call this function please. I want to get category count and category name from 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
  • You did not mention the issue. Are you able to get a response from the API using fetch or is it that you have the response and it is not rendering correctly? Commented Apr 17, 2020 at 4:02

1 Answer 1

2

You cannot call a function like that change

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

to

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

This will call the function when we press on click text.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.