how to use useState inside inner function scope in react.js hooks
You cannot access the house
state immediately after setting it, setHouse
may be batched.
See more of State: https://reactjs.org/docs/state-and-lifecycle.html
You are trying to do
let houseData = JSON.parse(res);
// houseData = { name: 'sweet house', address: 'fukuoka hakata' }
setHouse({ ...house, houseData: houseData });
instead of setHouse(houseData)
. Since houseData
is an Object, you can directly set it.
Replace
setHouse({...house, houseData})
with
setHouse(houseData)