【发布时间】:2019-05-21 09:04:40
【问题描述】:
这是我第一次开发 React 应用程序。
我将使用 axios 的 get 响应来填充表格。外部 var 是在 axios 范围之外声明和使用的,当我在 axios 范围内更新该 var 时,它不会更新外部 var 数据,如检入调试。
有没有办法在全局范围内/外部使用 axios 的 get 响应?
非常感谢您的帮助。
//rows is declared outside the scope
let rows = [];
axios
.get(`url here`)
.then(res => {
for (let i = 0; i < res.data.length; i++) {
//rows.push not reflecting on the react application (inside scope of res)
rows.push(res.data[i]);
}
});
//rows.push reflecting on the react application (outside scope of res)
rows.push({
test: 5.7
});
【问题讨论】:
-
只要在异步请求完成后访问
rows,您的代码就可以正常工作。
标签: javascript reactjs frontend