Было довольно много вещей, которые, на мой взгляд, были неправильными в коде (по крайней мере, насколько я понимаю, как работают нативные и firebase-функции!)
Я добавил много комментариев. Надеюсь, что это имеет смысл:
import React, { Component } from 'react';
import { FlatList, StyleSheet, View } from 'react-native';
import Post from './Post';
import Firebase from 'firebase';
import 'firebase/firestore';
import 'firebase/database';
import { firebaseConfig } from './configFirebase';
export default class Posts extends Component {
constructor(props) {
super(props);
!Firebase.apps.length ? Firebase.initializeApp(firebaseConfig.firebase) : Firebase.app();
this.state = {
postList: []
};
}
// moved componentDidMount to top as per convention
componentDidMount() {
this.getPostData();
// this.getItemCount(); // I'm pretty sure you don't need this
// this.renderItem(); // this doesn't belong here
}
// use arrow function so references to 'this' are bound to the component
getPostData = () => {
const ref = Firebase.database().ref('/posts'); // use const. It doesn't change.
ref.on('value', snapshot => {
console.log('DATA RETRIEVED'); // move inside the callback, so only triggers if you do actually retrieve data
const postsObj = snapshot.val();
if (!postsObj) return console.warn('No data from firebase');
const postsArr = Object.values(postsObj);
this.setState({postList: postsArr});
});
};
// I don't think you need getItemCount...
// getItemCount() {
// Firebase.database().ref('/posts').on('value', snapshot => {
// const postCount = snapshot.key.length;
// this.setState({ postCount });
// });
// }
renderItem({ item: post, index }) {
return (
<Post
key={index} // changed this to index, because your post objects don't have keys. But if you can add unique keys then that will be better!
heading={post.heading}
description={post.description}
location={post.location}
/>
);
}
render() {
return (
<View style={styles.container}>
{/* Cleaned up FlatList... I think you got confused here */}
<FlatList
keyExtractor={(item, index) => index.toString()} // syntax was wrong. Fixed.
data={this.state.postList}
renderItem={this.renderItem}
/>
</View>
);
}
}
export const styles = StyleSheet.create({
container: {
borderWidth: 2,
borderRadius: 5,
backgroundColor: '#2bb76e',
flex: 1
},
txtInput: {
flex: 1,
margin: 5,
padding: 5,
borderWidth: 2,
fontSize: 20,
borderRadius: 5,
backgroundColor: 'snow'
}
});