1

My JSON file can be found using this link. The object "features" have a nested object called "properties", where I want to access the data from that object. I've tried to use the useEffect() hook from React and implemented that in the code below. I tried to get the "properties" sub object by implementing the following code: data.features.properties, but that returns undefined. What code am I implemented wrong or what logic is incorrect?

useEffect(() => {

fetch('https://www.vaccinespotter.org/api/v0/states/' + stateAbb + '.json')
  .then((response) => response.json())
  .then((json) => {
    setData(json);
  .catch((error) => console.error(error))
  .finally(() => setLoading(false));
}, [stateAbb]);

stateAbb is the state abbreviation for the state that the user selects in a text input on a different screen. propData seems to store the "features" object as I have used the alert() function and typeof() to determine that propData is an object.

I've tried to JSON.parse() and implemented some other StackOverflow answers, such as this and this. The effect still remains the same. data.features works as an object but data.features.properties returns undefined.

Any help would be appreciated! Thanks!

3
  • 1
    features is an array. Commented May 29, 2021 at 4:04
  • Thanks! So then how can I get the object from within that array? Commented May 29, 2021 at 4:05
  • There is no the object, there are many objects in that array. Commented May 29, 2021 at 4:06

1 Answer 1

0

React hooks doesn't allow async await in useEffect so you can create new function like this


useEffect(()=>{
  fetchData()
},[])

const fetchData = async ()=>{
  try{
    const response = await  fetch('https://www.vaccinespotter.org/api/v0/states/' + stateAbb + '.json')
    const json = await response.json()
    console.log(json); // your data is here!
  }catch(err){
   console.log(err)
  }
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.