簡體   English   中英

組件如何在React中將自身作為參數傳遞?

[英]How could a component pass itself as an argument in React?

我什至不確定這是否可行,但我說我有這個:

<p id='1' disabled={this.anyfunction(???)}>Hello wurld</p>

那是我的職責

anyfuction:function(component){
    console.log(component.id)
}

我應該將什么參數傳遞給函數? this行不通的,我嘗試過bind(this)但問題是該函數沒有被調用(盡管myfunction.bind(this)可以工作,但不能做類似myfunction().bind(this)事情, HTML呈現時調用)。

有任何想法嗎 ?

您可以像這樣傳遞來自渲染的參數

this.anyFunc.bind(this, 'my parameter')

除了使用bind ,還可以使用call

this.anyfunction.call(this, some, other, parameters);

但是,除非我缺少任何東西,否則我認為您想要的是:

class Component extends React.Component {
  render() {
    let isDisabled = this.anyFunction();

    return (
      <p id='1' disabled={ isDisabled }>Hello wurld</p>
    );
  }

  anyFunction() {
    // you can reference the component w/ this
    return this.props.somethingPassedIn;
  }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM