score:1

Accepted answer

since setdata is the asynchronous method, you can't get the updated value of data immediatley after setdata.

  const card = ({ cardref }) => {
  const [data, setdata] = usestate({});
  useeffect(() => {
    setdata({ ...cardref });
    console.log(data.main.temp); // data is still the old value which is initial empty {} value, so `data.main` is undefined and `data.main.temp` will be error.
  }, [cardref]);

you should get it inside useeffect with adding a data dependency.

useeffect(() => {
  if (data.main) { // make sure that you need to check if `data.main` is not undefined before referring `data.main.temp`.
    console.log(data.main.temp);
  }
}, [data]);

Related Query

More Query from same tag