I want to display some properties of a JSON object which are in a locally stored JSON file. So, I'm trying like this:
import { DistroList as distroList } from '../backend/distroList.json'
useEffect(() => {
for(let distro in distroList) {
if(distroList[distro].Name === distroName) {
setDistroFeature(distroList[distro])
}
}
}, [])
return (
<div>
<h1>{distroFeature.Origin}</h1>
</div>
)
Since the for loop takes time to find the matched object and the component is mounted by then, I'm getting the error as
TypeError: Cannot read property 'Origin' of null
I tried to use fetch API and tried hard to make it asynchronous. I couldn't do it. I want to do something like this:
import { DistroList as distroList } from '../backend/distroList.json'
useEffect(() => {
// Fetch the object from distroList.json
// Update state variable
}, [])
return(
Display the updated state variable
)
How do I achieve it?
Thanks in advance :)
distroFeature.OrigintodistroFeature?.Origin