I was wondering if you could comment on best practices in the scenario below: I have a react page and am using an async call to get some data. On the page, I render some of that data that is returned like so. (setTimeout is in there so i can see it load - also API is public so you can see what it returns)
App.js
function App() {
const [data, setData] = useState([]);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
setTimeout(() => {
fetch("https://swapi.dev/api/people/1").then((response) =>
response.json().then((data) => {
setData(data);
setIsLoading(false);
console.log(data);
})
);
}, 2000);
}, []);
return (
<React.Fragment>
<p>{data.name}</p> //will be blank until aysnc call completes. THIS IS FINE.
<p>{data.films[0]}</p> //throws an error - trying to access an index that doesn't exist. PROBLEM
</React.Fragment>
);
}
I found a way around this, but I litter my code EVERYWHERE with the following. Plus I have to do it again in child components that i pass this data to.
<p>{isLoading ? "loading..." : data.films[0]}</p>
Does anyone know a better way or best practice for this?