2015-05-25 3 views
4

Я использую onRightButton в NavigatorIOS, отвечающем требованиям. Я хотел бы иметь возможность вызвать функцию, которая находится в компоненте, который я нажимаю, но я не могу понять, как этого достичь. Это пример кода:Функция для вызова на RightButtonPress в NavigatorIOS - React Native

this.props.navigator.push({ 
    component: SingleResultView, 
    title: "SomeTitle", 
    rightButtonIcon: require('image!mapsmarker'), 
    passProps: { 
    userPosition: this.props.userPosition 
    }, 
    onRightButtonPress:() => { SingleResultView.foo(); } // NOT WORKING! 
}); 

Как я могу это сделать?

ответ

0

SingleResultView еще не создан, поэтому у вас нет доступа к его методам.

Следующая будет работать:

this.props.navigator.push({ 
    onRightButtonPress: SingleResultView.prototype.foo 
}); 
5

ref обратного вызова проп ваш друг! https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute

goToResults() { 
    this.props.navigator.push({ 
     component: SingleResultView, 
     title: "SomeTitle", 
     rightButtonIcon: require('image!mapsmarker'), 
     passProps: { 
     userPosition: this.props.userPosition, 
     ref: this.onResultsRef, 
     }, 
     onRightButtonPress:() => { this._resultsView && this._resultsView.foo(); } 
    }); 
    } 

    onResultsRef(resultsViewRef) { 
    this._resultsView = resultsViewRef; 
    } 
Смежные вопросы