-1
I need to get data from a array
of the server to display to the user. A is a show as if it were a feed
of a social network, but is showing the same contents over and over again.
componentDidMount = async () => {
await api.get('postagens').then(res=>{
this.setState({id_postagens: res.data.count});
this.setState({posts: res.data.posts});
});
for(let i = 0; i<=this.state.id_postagens; i++){
const {titulo, criador} = this.state.posts[i];
this.setState({titulo,criador});
}
};
render() {
var valor = this.state.id_postagens;
var i=0;
var postagens = [];
while(i<valor){
const {criador, titulo} = this.state;
postagens.push(<View styles={{flex:1}} key= {i}>
<View style={styles.box}>
<View
style={styles.foto}>
</View>
<Text style={styles.texto}>{criador}</Text>
<Text style={styles.tempo}>30 min.</Text>
<View>
<Text style={styles.titulo}>{titulo}</Text>
</View>
<TouchableOpacity style={styles.iconComment}>
<Icon name="message-circle" size={25} color="#31788A" />
<Text style={styles.message}>0</Text>
</TouchableOpacity>
</View>
</View>)
i++;
}
return (
<ScrollView>
<View>
{postagens}
</View>
</ScrollView>
)
}
if code is not usual in the world
React
, an example is to usewhile
really very different, I’m not saying it can’t work, but it’s different than what we see around. In the function where you placed the request and then the status update of several variables does not give time to later execute afor
... there’s something strange about your code and that’s why, could improve understanding with an issue?– novic
Try to put this counter out of render, because React renders the page several times and I think that at each rendering it is initiating the variable
– Rafael Costa