简体   繁体   中英

how do I access data inside object?

I have set a JSON object inside local storage, and am able to get it and set it inside vaccine_data state but whenever I try to call any item inside the object, am getting undefined as value.

//state
const [vaccine_data, setVaccine_data] = useState([]);

//localstorage data
useEffect(() => {
    setVaccine_data(localStorage.getItem('data'));
    console.log('+++--vaccine_data');
    console.log(vaccine_data);
  // console returning
/*
{"startDate":1,"endDate":7}
  });
*/
}

//inside return
return (

    {console.log('+++++',vaccine_data)} // returning data
     {vaccine_data && vaccine_data!='' && vaccine_data!=null &&(
              console.log('aaaa',vaccine_data.startDate)
            )} // returning undefined

)

Have provided the code that specifies my doubt, let me know if anything else is required.

Please let me know how to access inside the object. Any help would be appreciated

localStorage.getItem('data') returns a string. try: JSON.parse(localStorage.getItem('data'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM