I am trying to return a String value to display it in my HTML. In my HTML, I'm using a Text element in a View (similar to a div in React Native) and try to fill it with that that I'm getting from the method _getCategories. The problem is that I can't make the method return a String value, since the value is returned inside a Promise.
Now, I'm trying to make this method return a simple String value:
_getCategories = item => {
const categoryNames = [];
let fetches = [];
var allCategories = '';
for (var i = 0; i < item.categories.length; i++) {
allCategories = allCategories + item.categories[i] + ',';
}
allCategories = allCategories.substring(0, allCategories.length - 1);
fetches.push(
fetch(
'http://54.168.73.151/wp-json/wp/v2/categories?include=' + allCategories
).then(response =>
response.json().then(responseJson => {
var names = '';
for (let category of responseJson) {
names = names + category.name + ', ';
}
this.names = String(names.substring(0, names.length - 2));
console.log(this.names);
return <Text style={styles.categories}>{this.names}</Text>;
})
)
);
};
I thought this returns the correct value already, but it's not showing in my View. I call the method above as follows:
<View>
{this._getCategories(item)} //this will be a text element after return
</View>
The issue can be reproduced here: