大家好,我是新来的反应,我被困在某个项目上。事情是,我有一个api_urlin this.props从父组件收到。在这个子组件中,我想使用api_url来使用JSON获取一些数据。在反应中访问this.props ComponentDidMount
在父组件我有
Repositories api_url={this.state.objs.repos_url}
和子组件,我想是这样
componentDidMount() {
$.getJSON(this.props.api_url, function(json) {
for (var i = 0; i < json.length; i++) {
var each_repo = json[i]
console.log(each_repo["name"]);
}
});
}
所以我需要的是在$ .getJSON的URL部分相应api_url 。
有没有办法在componentDidMount中访问this.props还是有一些其他方法来实现相同的结果吗?
另一件关于这个问题的是,我还在父组件的ComponentDidMount中使用$ .getJSON调用。 提前致谢。
我不确定完全明白... this.props在整个课程中都可用。所以你的代码是OK的。 –
你已经可以,你的意思是'$ .getJSON'吗?.. –
你的代码工程 - http://jsbin.com/wirodemulu/edit?html,js,console,output - 如果你想访问'this'从'$ .getJSON'中的回调函数中,您需要使用'.bind()'。 –