我是个新手,无法做出反应,并且陷入了某个项目。问题是我有一个api_url
in this.props
从父组件接收。在这个子组件中我想使用api_url
使用 JSON 获取一些数据。
在父组件中我有:
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"]);
}
});
}
所以我需要的是相应的api_url
在 url 部分$.getJSON
.
有没有办法访问this.props
in componentDidMount
或者还有其他方法可以达到相同的结果吗?
关于这一点的另一件事是,我也在使用$.getJSON
调用ComponentDidMount
父组件的。
提前致谢。
在你的类中实现构造函数:
constructor(props){
super(props);
this.state = {
//states
};
}
componentDidMount(){
//Get this.props
console.log(this.props.nameOfProp);
};
可能在这个反应版本更容易得到它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)