React Native Flatlist is not rendering results

Asked

Viewed 238 times

-2

I’m trying to show a box with a result of an Axios request using Flatlist but the screen is just blank, no error or anything.

export default class Home extends Component {
  state = {
    posts: [],
  };

  componentDidMount = ()=>{
    this.getPostagem();
  };

  getPostagem= async ()=>{
    const id = await AsyncStorage.getItem('id');
    await api.get("postagens").then( res => {
      this.setState({ posts: res.data.posts });
    });
  };

  renderItem = ({ item }) => (
    <View style={styles.listItem}>
      <Text>{item.criador}</Text>
    </View>
  );

  render() {
    return (
      <FlatList
        style={{ marginTop: 30 }}
        contentContainerStyle={styles.list}
        data={this.state.posts}
        renderItem={this.renderItem}
        //keyExtractor={item => item.texto}
      />
    );
  }
}

1 answer

-1

<View style={styles.listItem}>
        <FlatList
          data={posts}
          keyExtractor={item => item.id.toString()}
          renderItem={({ item }) => (
           <Text>{item.criador}</Text>
          )}
        />
</View>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.