我正在尝试从 json 中检索一些数据,以便稍后在图表中使用。
我的问题是使用useState,它让我永远不会输入数据,所以我无法访问字段。
const [myData, setData] = useState([]);
const url = 'url';
axios.get(url)
.then(res => {
setData(res.data);
myData.forEach( e=> {
console.log(e.name);
})
}).catch(function (error) {
// handle error
})
当我打印console.log时,名称会导致错误,因为“从不”类型上不存在属性“名称”。
我该如何解决这个问题?
编辑:我正在使用打字稿