2017-01-13 93 views
1

我在es6 +中介紹自己,我很難嘗試將一個函數道具傳遞給另一個組件。如何在React.Component上正確傳遞另一個組件的功能道具?

這是我的代碼:

class ProductList extends React.Component { 
    constructor(props) { 
     super(props); 
     this.onVote = this.handleProductUpVote.bind(this); 
    } 
    handleProductUpVote(productId) { 
     console.log(productId +" was upvoted.") 
    } 
    render() { 
     const products = Data.map((product) => { 
      return (
       <Product 
        key={'product-'+product.id} 
        id={product.id} 
        title={product.title} 
        description={product.description} 
        url={product.url} 
        votes={product.votes} 
        submitter_avatar_url={product.submitter_avatar_url} 
        product_image_url={product.product_image_url} 
        onVote={this.handleProductUpVote} 
       /> 

      ); 
     }); 

     return (
      <div className="ui items"> 
       {products} 
      </div> 
     ); 
    } 
} 

我要傳遞的功能onVote在此組件(產品)

class Product extends React.Component { 
    handleUpVote() { 
     this.props.onVote(this.props.id).bind(this) /* the error is here, I am trying 
to pass the id props, and invoke the onVote prop here */ 
    } 
    render() { 
     return (
      <div className="item"> 
       <div className="image"> 
        <img src={this.props.product_image_url} /> 
       </div> 
       <div className="middle aligned content"> 
        <div className="description"> 
         <a onClick={this.handleUpVote}> 
          <i className="large caret up icon"/> 
         </a> 
         {this.props.votes} 
        </div> 
        <div className="description"> 
         <a href={this.props.url}> 
         {this.props.title} 
         </a> 
        </div> 
        <div className="extra"> 
         <span> Submitted by: </span> 
         <img 
          className="ui avatar image" 
          src={this.props.submitter_avatar_url} 
         /> 
        </div> 
       </div> 
      </div> 
     ); 
    } 
} 

我在這裏等道具沒問題。我試圖調用handleUpVote函數,我用它綁定,但我不能讓它工作。幫幫我?

回答

1

你,當你把它傳遞給產品組件使用界handleProductUpVote方法。

正如您在構造函數中看到的那樣,您已經綁定了它並將其分配給this.onVote屬性。

有2個解決方案:

  1. 您應該使用onVote={this.onVote}在渲染方法。
  2. 將構造函數中的屬性onVote的名稱更改爲this.handleProductUpVote。而你最終this.handleProductUpVote = this.handleProductUpVote.bind(this)http://reactkungfu.com/2015/07/why-and-how-to-bind-methods-in-your-react-component-classes/

    更新的渲染方法(即onVote={this.handleProductUpVote})離開分配

更多信息:

和更新您的產品類別:

class Product extends React.Component { 
    constructor(props) { 
     super(props); 
     this.handleUpVote = this.handleUpVote.bind(this); 
    } 
    handleUpVote() { 
     this.props.onVote(this.props.id) 
    } 
    // the render method 
} 
+0

所以這就是我所做的,在 構造(道具){ \t \t超級(道具); \t \t this.onVote = this.handleProductUpVote.bind(this); \t}和產品組件onVote = {this.onVote},在此類產品組件上調用, handleUpVote(){ \t \t this.props.onVote(this.props.id); \t},我仍然有「Uncaught TypeError:無法讀取null屬性的'道具',從你的答案中,這是我的理解。你能詳細說明我該怎麼做嗎?我真的很感激 –

+0

我已經用固定的產品組件更新了答案。你以錯誤的方式綁定handleUpVote,它應該在構造函數中完成。 –

0

在你的產品組件刪除在handleUpVote綁定(),只是調用它像this.props.onVote(this.props.id);

+0

我仍然有它的錯誤。 「未捕獲的類型錯誤:無法讀取null的屬性'道具'」 –

+0

哦,就像Przemyslaw所說,擴展我的答案,在你的ProductList組件中render()將它作爲像onVote = {this.onVote}而不是onVote = { this.handleProductUpVote} –

+0

所以這就是我所做的構造函數(道具){超級(道具); this.onVote = this.handleProductUpVote.bind(this); }和產品組件onVote = {this.onVote},像這樣在產品組件上調用,handleUpVote(){this.props.onVote(this.props.id); },我仍然有「Uncaught TypeError:無法讀取null屬性的'道具',從你的答案中,這就是我所理解的。你能詳細說明我該怎麼做嗎?我真的很感謝 –

相關問題