2015-04-07 16 views
1

我有一個視圖,我希望用戶能夠選擇多個項目。選擇項目後,我希望他們點擊右上角的完成,然後轉到下一個屏幕。使用Swift我迷上了prepareForSegue和segue.destinationViewController來實現它。NavigatorIOS onRightButtonPress訪問查看數據

使用React Native,我使用NavigatorIOS onRightButtonPress函數,但不確定如何訪問View數據以在passProps中使用。

的代碼目前看起來是這樣的:

return (<NavigatorIOS 
    ref="nav" 
    style={styles.nav} 
    initialRoute={{ 
    nav: this.refs.nav, 
    component: this.props.onSuccessView, 
    title: 'What Would You Like To Do?', 
    rightButtonTitle: 'Next', 
    onRightButtonPress:() => {this.refs.nav.navigator.push({ 
      title: "Where To Go", 
      backButtonTitle: 'Back', 
      component: SuggestionsView 
     }); 
    } 
    }} 
/>); 

我要的是this.props.onSuccessView.onRightButtonPress處理哪個組件應該叫並提供道具。這可能嗎?

如果不是,我可以調用一個函數,如道具:this.props.onSuccessView.getProps()?

歡迎任何建議!

感謝

回答

1

這種氣味怪異。你確定這是你想要做的嗎?爲什麼不傳遞來自父組件的回調,以便知道你在這裏展示的孩子 - 然後你可以通過抓住孩子並對他們採取行動來爲你提供所需的資源。

或者:查看NavigatorIOS的the example,看看路由器是如何使用的 - 彈出和推送方法確實覺得你更適合在這裏做什麼。

+0

謝謝,它可能是一種氣味。我不確定回調是如何工作的,你有任何例子嗎?你的意思是類似於passProps:{route:this.props.route}或https://github.com/facebook/react-native/blob/master/Examples/UIExplorer/createExamplePage.js? 我的想法是,我希望視圖本身能夠對下一步和下一步的數據負責,而不是對視圖進行初始化。 –

1

A.在初始分量

this.props.navigator.push({ 
    title: 'title', 
    component: MyComponent, 
    rightButtonTitle: 'rightButton', 
    passProps: { 
     ref: (component) => {this.pushedComponent = component}, 
    }, 
    onRightButtonPress:() => { 
     // Access View Data 
     this.pushedComponent && this.pushedComponent.props.someObject...; 
    }, 
}); 

B.在推部件
替換onRightButtonPress FUNC推入部件。

componentDidMount: function() { 
    // get current route 
    var route = this.props.navigator.navigationContext.currentRoute; 
    // update onRightButtonPress func 
    route.onRightButtonPress = () => { 
     // Access View Data 
     this.props.someObject... 
    }; 
    // component will not rerender 
    this.props.navigator.replace(route); 
},