2016-09-27 50 views
0

下面是我的應用程序組件:如何將導航器對象傳遞給反應原生根文件中使用的組件?

/** 
* Class app from where the app bootstraps 
*/ 
export default class App extends Component { 
    constructor(props) { 
    super(props); 
    } 

    // This is where all your routes will be processed 
    renderScene(route, navigator) { 
    // Set a variable to get the route 
    let RouteComponent = route.component; 

    _navigator = navigator; 

    // With props return the components 
    return (
     <RouteComponent navigator={navigator} {...route.passProps} /> 
    ); 
    } 

    static navigationBarRouteMapper = openControlPanel => ({ 
     LeftButton: function(route, navigator, index, navState) { 
     return (

      // Hamburger icon which on clicked opens the menu 
      <TouchableOpacity style={navBarStyle.left} onPress={() => openControlPanel()}> 
      <View> 
       {hamburgerIcon} 
      </View> 
      </TouchableOpacity> 
     ); 
     }, 
     RightButton: function(route, navigator, index, navState) { 
     return (
      // cart menu 
      <TouchableWithoutFeedback onPress={() => dismissKeyboard()}> 
      <View style={navBarStyle.right}> 
       {cartIcon} 
       <View style={navBarStyle.counter}> 
       <Text style={navBarStyle.counterText}>20</Text> 
       </View> 
      </View> 
      </TouchableWithoutFeedback> 
     ); 
     }, 
     Title: function(route, navigator, index, navState) { 
     // Title of the route 
     return (
      <TouchableWithoutFeedback onPress={() => dismissKeyboard()}> 
      <View style={navBarStyle.titleWrap}> 
       <Text style={navBarStyle.title}>{route.title.toUpperCase()}</Text> 
      </View> 
      </TouchableWithoutFeedback> 
     ); 
     } 
    }) 

    // Close the menu drawer 
    closeControlPanel() { 
    this._drawer.close(); 
    } 

    // open the menu drawer 
    openControlPanel() { 
    this._drawer.open(); 
    dismissKeyboard(); 
    } 

    // On clicking the menu item, this function routes to that menu item page 
    getNavigator(route) { 
    this.refs.navigator.replace(route); 
    this.closeControlPanel(); 
    } 

    render() { 

    return (
     <Drawer 
     ref={ (ref) => { this._drawer = ref; } } 
     type="overlay" 
     content={<Menu navigator={this.getNavigator.bind(this)} menuItems={menu} closeControlPanel={this.closeControlPanel.bind(this)} />} 
     onOpenStart={() => dismissKeyboard()} 
     tapToClose={true} 
     openDrawerOffset={0.2} 
     panCloseMask={0.2} 
     panOpenMask={20} 
     acceptPan={true} 
     closedDrawerOffset={-3} 
     styles={drawerStyle} 
     tweenHandler={(ratio) => ({ 

      // This code will maintain the opacity for main 
      // Whilst the opacity for the mainOverlay on the screen will be faded. 
      main: { opacity: 1 }, 
      mainOverlay: { 
      opacity: ratio/2, 
      backgroundColor: 'black', 
      } 

     })}> 

     <Navigator 
      initialRoute={homeScene} 
      renderScene={this.renderScene} 
      ref="navigator" 
      // Navbar of the app 
      navigationBar={ 
      <Navigator.NavigationBar 
       routeMapper={App.navigationBarRouteMapper(this.openControlPanel.bind(this))} 
       style={navBarStyle.navBar} 
      /> 
      } 
     /> 

     <EditSearch /> 
     </Drawer> 
    ); 
    } 
} 

在最底層,你會看到<EditSearch>組件包含兩個文本輸入。除了主頁之外,該組件對於應用中的所有頁面都是通用的。

因此,我想知道我目前在哪個頁面或場景上,以便我可以檢查場景/頁面是否是主頁。如果是主頁,我會隱藏組件,否則我會顯示所有其他頁面。

我試圖通過裁判通過導航像這樣:

<EditSearch nav={this.refs.navigator} /> 

但是,我得到的<EditSearch>組件上未定義和視圖不重新渲染時,頁面的變化,因爲它沒有檢測到任何狀態變化。

我可以這樣做:

this.state = { 
    currentRoute: 'home' 
} 

然後改變這種狀態路由變化時。但是,我無法在renderScene中更改狀態,因爲renderScene中的設置狀態會導致無限循環。如果我可以在路由更改時將頁面標題設置爲此狀態,則可以將該狀態發送到<EditSearch>組件。

我很困惑如何將當前路線信息傳遞給這個通用組件。感謝預期。

回答

0

好的,我找到了一個解決方案,但我不確定它是否合適。

下面是我的解決方案。

首先,我創建的狀態和功能設置狀態:

constructor(props) { 
    super(props); 

    this.state = { 
     isHome: false 
    }; 

    this.setIsHome = this.setIsHome.bind(this); 
    } 

    setIsHome(flag) { 
    // Set state to let component know whether the page is home or not 
    this.setState({ 
     isHome: flag ? flag : false 
    }); 
    } 

然後函數傳遞給所有的網頁,這些網頁可以隱藏或顯示<EditSearch>組件:

<RouteComponent navigator={navigator} {...route.passProps} setIsHome={this.setIsHome} /> 

然後我通過isHome狀態到<EditSearch>組件:

<EditSearch isHome={ this.state.isHome } /> 

這是我如何調用在家裏組件的功能:

componentWillMount() { 
    this.props.setIsHome(true); 
    } 

    componentWillUnmount() { 
    this.props.setIsHome(false); 
    } 

我這是怎麼顯示/隱藏組件。我希望這有幫助。但是,知道如何將導航器對象傳遞給這些組件真的很不錯。

0

下面是抽屜Github問題的解決方案。

找到了一個快速的解決方法,但它並不理想。如果您將 Navigator與Drawer組件包裝起來,那麼在 Navigator.NavigationBar中可以設置this.navigator = this.navigator || 導航器,然後您可以將導航器傳遞到抽屜的內容 組件。

<Drawer 
    content={<Main navigator={this.navigator}/>} 
    type='overlay'> 

    <Navigator 
    renderScene={renderScene} 
    navigationBar={ 

     <Navigator.NavigationBar 
     routeMapper={{ 
      LeftButton: (route, navigator, index, navState) => { 

      this.navigator = this.navigator || navigator; 

      return (
       <View> 
       <Text>Something</Text> 
       </View> 
      ); 
      } 
     }} 
     /> 
    /> 
</Drawer> 

https://github.com/root-two/react-native-drawer/issues/187#issuecomment-231461585

相關問題