2017-06-08 1 views
0

コンポーネントから親メソッドを呼び出して状態を更新しようとしていて、状態を更新していないようです。コンソールログが実行されているときに関数が呼び出されていますが、状態は更新されていません。親メソッドの呼び出しが機能しないのはなぜですか?状態が更新されない

<CustomerTable />コンポーネントにあるshowFirstFive()関数を、その子コンポーネント内で実行しようとしています - <CustomerEntry />(小道具として渡されました)。

上部のボタンは状態を良好に変更しますが、子コンポーネント内のテーブルのボタンは変更しません。

ご協力いただければ幸いです。あなたがそのメソッドを呼び出す必要が

codepen here

class CustomerEntry extends React.Component { 
    modifyState(e) { 
     e.preventDefault(); 
     console.log("test"); 
     this.props.changeState; 
    } 

    render() { 
     return (
      <tr> 
       <td>{this.props.name}</td> 
       <td>{this.props.email}</td> 
       <td>{this.props.postcode}</td> 
       <td> 
        {this.props.product} 
        <button onClick={this.modifyState.bind(this)}>Show first 5 items</button> 
       </td> 
      </tr> 
     ); 
    } 
} 

class CustomerTable extends React.Component { 
    constructor(props) { 
     super(props); 
     let customers = this.props.customers; 
     this.state = { 
      customers 
     }; 
    } 

    showFirstFive() { 
     let customers = this.props.customers; 
     customers = customers.slice(0, 5); 
     this.setState({ customers }); 
    } 
    showAll() { 
     let customers = this.props.customers; 
     this.setState({ customers }); 
    } 

    render() { 
     console.log("this.props.customers", this.props.customers); 
     let customerList; 

     let thisComp = this; 

     if (this.props.customers) { 
      customerList = this.state.customers.map(function(customerData) { 
       return (
        <CustomerEntry 
         key={customerData.key} 
         changeState={thisComp.showFirstFive.bind(this)} 
         index={customerData.key} 
         name={customerData.name} 
         email={customerData.email} 
         postcode={customerData.postcode} 
         product={customerData.product} 
        /> 
       ); 
      }); 
     } 

     return (
      <div> 
       <button onClick={this.showFirstFive.bind(this)} >Show first 5 items</button> 
       <button onClick={this.showAll.bind(this)} >Show all</button> 
       <table> 
        <thead> 
         <tr> 
          <th>Name</th> 
          <th>Email</th> 
          <th>Postcode</th> 
          <th>Product</th> 
         </tr> 
        </thead> 
        <tbody> 
         {customerList} 
        </tbody> 
       </table> 
      </div> 
     ); 
    } 
} 


class App extends React.Component { 
    constructor(props) { 
     super(props); 
     this.state = { 
      customers: [ 
       { 
        key: "86464", 
        name: "Mr John Smith", 
        email: "[email protected]", 
        postcode: "LN47AT", 
        product: "apples", 
       }, 
       { 
        key: "787862", 
        name: "Miss Sarah Jackson", 
        email: "[email protected]", 
        postcode: "DN54LQ", 
        product: "apples", 
       }, 
       { 
        key: "4454564", 
        name: "Dr Hugh James", 
        email: "[email protected]", 
        postcode: "S102AT", 
        product: "bananans", 
       },    { 
        key: "11123134", 
        name: "Mr John Smith", 
        email: "[email protected]", 
        postcode: "LN47AT", 
        product: "apples", 
       }, 
       { 
        key: "68868", 
        name: "Miss Sarah Jackson", 
        email: "[email protected]", 
        postcode: "DN54LQ", 
        product: "apples", 
       }, 
       { 
        key: "6876871", 
        name: "Dr Hugh James", 
        email: "[email protected]", 
        postcode: "S102AT", 
        product: "bananans", 
       }, 
           { 
        key: "17117", 
        name: "Mr John Smith", 
        email: "[email protected]", 
        postcode: "LN47AT", 
        product: "apples", 
       }, 
       { 
        key: "17171716", 
        name: "Miss Sarah Jackson", 
        email: "[email protected]", 
        postcode: "DN54LQ", 
        product: "apples", 
       }, 
       { 
        key: "151571", 
        name: "Dr Hugh James", 
        email: "[email protected]", 
        postcode: "S102AT", 
        product: "bananans", 
       }, 

      ] 
     }; 
    } 

    render() { 
     return (
      <div> 
       <CustomerTable customers={this.state.customers} /> 
      </div> 
     ); 
    } 
} 

ReactDOM.render(<App />, document.getElementById("app")); 

答えて

2

()を逃した:

modifyState(e) { 
    e.preventDefault(); 
    console.log("test"); 
    this.props.changeState(); //here 
} 

提案者:

代わりの方法を結合

onClick = {this.showFirstFive.bind(this)} 

コンストラクタでそれを定義します。

this.showFirstFive = this.showFirstFive.bind(this); 

次に、このようにそれを使用します。別の変数にthisキーワードの参照を保存するonClick = {this.showFirstFive}

2.が必要とされていない、あなたが使用していますarrow functionこのように:

customerList = this.state.customers.map((customerData) => { 
     //now you can access this 
} 
+0

スーパークイック返信メイトありがとう!それは完全にそれをソート!矢印機能のお手伝いもありがとう!心から感謝する!乾杯! – Ash

+0

反応状態のフィルタリングに関するアドバイスはありますか?検索+列ソート+ページ設定を設定する必要があります。私はちょうどJSオブジェクトの変更と状態を更新すると思います。任意のリンク/アドバイスはv歓迎です! – Ash

+1

'react-bbotstrap-table'を使うことをお勧めします。デフォルトでは、** search、sorting、pagination **など多くのクールな機能が用意されています。ドキュメントと例を確認してください:http://allenfang.github.io/react-bootstrap-table/index.html –

関連する問題