简体   繁体   English

在 react ComponentDidMount 中访问 this.props

[英]Accessing this.props in react ComponentDidMount

I am new to react and i am stuck on a certain project.我是新手,我被困在某个项目上。 The thing is that I have an api_url in this.props received from a parent component.事情是,我有一个api_urlthis.props从父组件接收。 And in this child component I want to use api_url to fetch some data using JSON.在这个子组件中,我想使用api_url来使用 JSON 获取一些数据。

In the parent component I have:在父组件中,我有:

Repositories api_url={this.state.objs.repos_url}

and in the child component, I want something like:在子组件中,我想要类似的东西:

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"]);
        }    
    });
}

so what I need is the corresponding api_url in the url section of $.getJSON .所以我需要的是相应的api_url中的URL部分$.getJSON

Is there a way to access this.props in componentDidMount or is there some other way to achieve the same results?有没有办法在componentDidMount访问this.props或者有其他方法可以达到相同的结果?

Another thing regarding this, is that I'm also using a $.getJSON call in the ComponentDidMount of the parent component.关于此的另一件事是,我还在父组件的ComponentDidMount中使用了$.getJSON调用。

A thanks in advance.提前致谢。

In your class implement the constructor:在你的类中实现构造函数:

constructor(props){
  super(props);
  this.state = {
     //states
  };
}

componentDidMount(){
  //Get this.props
  console.log(this.props.nameOfProp);
};

Probably at this react version is more easy to get it.可能在这个反应版本中更容易获得它。

In order to access Props in the child component you need to create a constructor in the child component and then pass props as an attribute to the super() function as为了访问子组件中的 Props,您需要在子组件中创建一个构造函数,然后将 props 作为属性传递给 super() 函数,如下所示

constructor(props) {
   super(props);
   this.state = {}
}
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"]);

      }

    });

  } 

Once you do this you can access props in your entire child component完成此操作后,您可以访问整个子组件中的道具

You probably have a problem with the this inside your ajax call.您的 ajax 调用中的this可能有问题。 My guess would be that you have to bind this in your Repositories so that the instance of this in your ajax call refers to Repositories and not the ajax object.我的猜测是您必须将this绑定到您的存储库中,以便您的 ajax 调用中的this实例引用存储库而不是 ajax 对象。

For those who stumble on this page now, what's missing above is .bind(this) after $.getJSON .对于那些现在$.getJSON这个页面的人来说,上面缺少的是$.getJSON之后的.bind(this) Otherwise you can't access this within $.getJSON .否则你不能在$.getJSON访问this

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"]);
        }

    }).bind(this);
}

My issue is similar but resolve this way我的问题很相似,但可以通过这种方式解决

    let self = this
    this.props.users.map(function (u, x) {
        if (self.props.data[u.name]) {
            //Stuff
        }
    }

My if statement was not recognising props so used self instead我的 if 语句不能识别 props 所以用 self 代替

Have you tried adding this.componentDidMount = this.componentDidMount.bind(this);你有没有试过添加this.componentDidMount = this.componentDidMount.bind(this); to your constructor?给你的构造函数? This way you can reach this from componentDidMount .通过这种方式,您可以从componentDidMount访问this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM