React Native Newbie, I’m getting my data as printed in the console, but no list items are being rendered? test
is my data within my useState
.
Here’s my code:
export default function HomeScreen({ navigation }) {
const [loading, setLoading] = useState(true);
const [test, setTest] = useState([]);
let loadData = async () => {
const q = query(collection(FIREBASE_FIRESTORE, "test"));
const qSnap = await getDocs(q);
let testData = [];
qSnap.forEach((doc) => {
let documentData = doc.data();
documentData.id = doc.id;
test.push(documentData);
});
setTest(testData);
setLoading(false);
};
if (loading) {
loadData();
return <ActivityIndicator style={style.indicator} />;
} else {
return (
<View style={style.container}>
<FlatList
alwaysBounceVertical={true}
keyExtractor={(item) => item.id}
data={test}
renderItem={({ item }) => (
<View style={style.cell}>
<Text style={style.text}>{item.title}</Text>
</View>
)}
/>
</View>
);
}
}
2
Answers
Calling the
loadData()
in the component body is not correct. You should useuseEffect
to fetch data. Also, you createdtestData
, but you are only pushing totest
in yourforEach
. This should work: