i am writing perfect code but its not rendering
import axios from "axios";
import React, { useEffect, useState } from "react";
import { View, StyleSheet, ScrollView, FlatList } from "react-native";
import AppCard from "../components/AppCard";
import { Avatar, Button, Card, Text } from "react-native-paper";
function Home() {
const [data, setData] = useState();
useEffect(() => {
async function fetchData() {
try {
const data = await axios.get("https://fakestoreapi.com/products");
setData(data);
} catch (error) {
console.log(error);
}
}
fetchData();
}, []);
return (
<View>
<FlatList
data={data}
renderItem={({ item }) => (
<Card>
<Card.Cover source={{ uri: item.image }} />
<Card.Content style={{ margin: 19 }}>
<Text variant="titleLarge">{item.title}</Text>
</Card.Content>
</Card>
)}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
export default Home;
i wrote a clean code but its not working at all
llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll
2
Answers
try this one
In your useEffect, you are setting the fetched data directly to the data state variable. However, you should set the actual data from the API response, which is typically found in the data property of the response object. To fix this, you should change setData(data) to setData(data.data):