I have a screen which should display a result of search. So, I created my fetch in the render(). As describing in this stackoverflow response, I pushed my response in an array. In the console panel, the result array is loading after some seconds but in my screen, the result array still empty.
This is my render function
render() {
let result = [];
let endPoint = this.props.navigation.getParam('endPoint', '');
fetch(endPoint, {
method: "GET",
headers: null,
}).then(response => response.json())
.then(response => {
let list = response;
for (let i = 0; i < list.length; i++) {
result.push(
<View key={i} style={styles.item}>
<View>
<View style={styles.imageContainer}>
<Image style={styles.imageItem} resizeMode="cover"
source={{uri: list[i].img}}/>
</View>
</View>
<View style={styles.inlineDetails}>
<Text style={styles.innerTitle}>
{list[i].lieugeo}
</Text>
<Text style={styles.innerDescription}>
{list[i].desc.substring(0, 80)}
</Text>
</View>
</View>
)
}
}).catch((error) => {
console.error(error);
});
return (
<ImageBackground source={require('./images/internal_bg.png')} style={{width: '100%', height: '100%'}}>
<ScrollView>
<View style={styles.container}>
{result}
</View>
</ScrollView>
</ImageBackground>
);
}
Expected result: a list of items is displayed Actual result: the screen still empty