2017-05-23 6 views
0

親リストビューから子コンポーネント内の関数を呼び出すために:どのように私は(親コンポーネント)のListViewからのナビゲーションを持って

rowPressed(element) { 
    this.props.navigator.push({ 
      title: element.title, 
      rightButtonSystemIcon: 'action', 
    onRightButtonPress:() => { 
     // how to call my function _handleRightButtonPress() ? 
    }, 
      component: ElementDetail, 
      passProps: { 
      element: element 
      } 
     }); 
    } 

私はElementDetailコンポーネントで_handleRightButtonPress()を呼び出すしたいと思います:

class ElementDetail extends Component { 

    constructor(props) { 
    super(props); 
    ... 
    } 

    _handleRightButtonPress() { 
    console.log('right Button Pressed'); 
    }; 

... 

} 

これを行うにはどのようなアイデアですか?

答えて

0

最も簡単な方法は、親が関数を子に渡すことです。子は、その関数を使用してその親と通信することができます。

親はこのように、小道具として子供に関数を渡す:

<MyChild myFunc={this.handleChildFunc.bind(this)} /> 

を、子はそうのように、その関数を呼び出します:間の通信に関する詳細については

this.props.myFunc(); 
And don't forget that the child will need this function declared in its propTypes: 

MyChild.propTypes = { 
    myFunc: React.PropTypes.func, 
}; 

をコンポーネントを参照してくださいLink

+0

ありがとうございます。しかし、私はどのように子コンポーネントでこの関数を "onRightButtonPress"(親クラスにある)から呼び出すことができますか? – peter

+0

あなたはreact-reduxを使用して、コンポーネントがredux経由で小道具を受け取ると子コンポーネントで呼び出されるいくつかのデータを送信することができます。 – Vicky

関連する問題