2017-10-25 5 views
0

this.props.childrenの小道具としてhandleCloseを渡す必要があります。この関数は子の状態にアクセスする必要があります。パラメータchildを渡す方法がわからないので、{handleClose: this.handleClose}に問題があります。this.props.childrenの子として子ステートを設定する関数を渡します。

class App extends React.Component { 
    constructor(props, context) { 
     super(props, context); 
     this.handleClose = this.handleClose.bind(this); 
    } 

    handleClose(child) { 
     if (child.state.redirect) { 
      browserHistory.push(url); 
     } else { 
      child.setState({showError: false, errorText: ''}); 
     } 
    } 

    render() { 
     const children = React.Children.map(this.props.children, (child) => { 
      return React.cloneElement(child, 
       {handleClose: this.handleClose}, this); 
     }); 

     return (
      <div> 
       {children} 
      </div> 
     ); 
    } 
} 

答えて

1

あなたは、パラメータ

React.cloneElement(child, {handleClose: function() { this.handleClose(child); }.bind(this)}, this); 

// OR 

React.cloneElement(child, {handleClose:() => this.handleClose(child)}, this); 
+0

おかげで機能を実行機能を渡すことができます!第二の変種が私を助けました。 – MyName

+0

これがあなたの質問に答えたならば、人々はそれが働いていて、質問が解決されたことを知るためにこれを正しい答えとしてください – Jayce444

関連する問題