javascript - 使用 React.js 将数据绑定到前端
问题描述
我对 react.js 库很陌生。我正在尝试制作一个简单的 CRUD 应用程序。
我制作了一个名为dataprovider.js
export function getCrudData() {
axios.get('https://api.github.com/users/fearcoder')
.then(response => {
this.setState({ githubdata: response.data });
})
.catch(function (error) {
console.log(error);
})
}
我已经导入了这个文件crudexample.js
并调用了这样的方法:
constructor(props) {
super(props);
dataprovider.getCrudData();
}
当我使用 F12 打开 Google 开发工具时,我可以看到 github 数据,因此工作正常。
现在我想绑定这些数据,我这样做了:
<td>{githubdata.bio}</td>
我在我的谷歌开发工具中收到以下错误
'githubdata' 未定义 no-undef
有人可以指出我正确的方向吗?
解决方案
尝试以下操作:
constructor() {
super(props);
this.state = {
githubdata: "" // Define githubdata.
}
}
componentDidMount() {
axios.get('https://api.github.com/users/fearcoder')
.then(response => {
this.setState({ githubdata: response.data });
})
.catch(function (error) {
console.log(error);
})
}
使成为:
<td>{this.state.githubdata.bio}</td>
推荐阅读
- reactor-netty - Netty Http客户端是JEE环境
- python - 在对任何数据结构执行某种方法时,何时将变量分配给自身?
- javascript - 如何在反应js中的组件渲染之前进行api调用
- javascript - 如何使用 javascript 在 mozilla firefox 中获取特定选项卡的 HTML
- java - 如何按自定义顺序对字符串数组进行排序?
- windows - 在 OAuth2 流程中,我们可以将身份验证委托给 Windows SSO
- apache-spark - 如何在 hive UDF 中获取 taskID 或 mapperID(类似于 Spark 中的 partitionID)?
- java - 使用 flink 作业读取 csv 文件
- tcl - TK/TCL 控制台不运行完整脚本,但可以手动输入
- open-liberty - 指定 FFDC 文件名