2017-01-05 34 views
2

我在我的應用程序中有一個標籤欄,我不希望導航轉換影響每次渲染新路線。因此我想將標籤欄放在導航器外面,但是如何在該情況下觸發導航操作?我無法訪問從導航器外部傳遞給renderScene函數的導航器對象。如何從導航器外部調用導航器?

這裏是返回在app.js:

  <View> 
       <Navigator 
        ref="navigator" 
        initialRoute={{name: "start"}} 
        renderScene={this.renderScene.bind(this)} 
        configureScene={this.configureScene.bind(this)} 
       /> 
       <TabBar navigator={???} style={styles.nav} /> 
      </View> 

回答

1

我不認爲這是你想要的東西的最佳方式。但是,爲了回答你的問題:

  <View> 
       <Navigator 
        ref="navigator" 
        initialRoute={{name: "start"}} 
        renderScene={this.renderScene.bind(this)} 
        configureScene={this.configureScene.bind(this)} 
       /> 
       <TabBar getNavigator={()=>this.refs.navigator} style={styles.nav} /> 
      </View> 

然後你可以調用getNavigator()從您的TabBar

+0

導航仍然是不確定的TabBar,所以你知道爲什麼嗎? – theva

+0

嘗試在TabBar組件中記錄'this.props.navigator'組件 –

+0

這就是我所做的,在那裏沒有定義 – theva

3

好吧,我想我解決了這個。問題似乎是在組件的第一次渲染時refs不可用。我解決了這個由:

<Navigator 
    ref={(r) => { this.navigatorRef = r; }} 
    initialRoute={{name: "start"}} 
    renderScene={this.renderScene.bind(this)} 
    configureScene={this.configureScene.bind(this)} 
/> 
{this.state ? <TabBar navigator={this.state.navigatorRef} style={styles.nav} /> : null } 

,並添加了這一點:

componentDidMount() { 
    this.setState({navigatorRef: this.navigatorRef}); 
} 

只是爲了讓組件呈現第二次用的TabBar。

1

這是另一個版本,爲我工作。它的hackish,將可能在未來打破,但如果你在忙亂之中可能有幫助;)

<View> 
 
    <Navigation 
 
    ref={(r) => { this.navigation = r }} 
 
    /> 
 

 
    <OtherScreen 
 
    navigation={(this.navigation || {})._navigation} 
 
    {...this.props} 
 
    /> 
 
</View>