react-native:如何结合react-native-router-flux和react-native-drawer?

时间:2017-03-13 12:53:05

标签: react-native react-native-android react-native-router-flux react-native-drawer

我正在尝试将react-native-router-fluxreact-native-drawer

进行整合

我的主要组件实际上是 App.js 。我只复制/粘贴相关的部分。

import AppRoutes from './Router';
...
constructor(props) {
    firebase.auth().onAuthStateChanged((user) => {
      if (user) {
        Actions.mainBucket({ type: 'reset' });
      } else {
        Actions.authBucket({ type: 'reset' });
      }
    });
    super(props);
  }
}
...
  render() {
    const store = createStore(
      reducers, 
      {}, 
      applyMiddleware(ReduxThunk)
    );

    return (
      <Provider store={store}>
        <AppRoutes />
      </Provider>
    );
  }

这是 Router.js

const scenes = Actions.create(
  <Scene key="root" hideNavBar open={false} >
    <Scene key="splashBucket">
      <Scene key="splashScene" component={SplashScreen} title="Splash Screen Title" hideNavBar />
    </Scene>
    <Scene key="authBucket">
      <Scene key="authScene" component={AuthForm} title="Accesso" hideNavBar={false} />
    </Scene>
    <Scene key="mainBucket" hideNavBar={false}>
      <Scene key="schedule" component={Schedule} title="Impegni"  />
      <Scene key="peopleList" component={PeopleList} title="Lista pesone"/>
    </Scene>
  </Scene>
);

const AppRoutes = () => (
  <Router 
    sceneStyle={{ paddingTop: 65 }} 
    scenes={scenes}
  />
);

export default AppRoutes;

我的目标是将SideDrawer添加到具有键“mainBucket”的场景桶中。,因为,正如您所看到的,这将是/将成为我的应用程序的主要部分,并且用户是使用firebase自动重定向到此存储桶实现自动登录。

自动登录和mainBucket的开启工作。

要开始使用添加sidedrawer,我认为sidedrawer内容组件最初只会呈现<Text>使用SideMenu组件

我该如何处理?

我创建了以下两个文件。

第一个是NavigationDrawer.js

export default class NavigationDrawer extends Component {
  render() {
    const state = this.props.navigationState;
    const children = state.children;
    return (
      <Drawer
        ref="navigation"
        open={state.open}
        onOpen={() => Actions.refresh({ key: state.key, open: true })}
        onClose={() => Actions.refresh({ key: state.key, open: false })}
        type="displace"
        content={<SideMenu />}
        tapToClose
        openDrawerOffset={0.2}
        panCloseMask={0.2}
        negotiatePan
        tweenHandler={(ratio) => ({
          main: { opacity: Math.max(0.54, 1 - ratio) }
        })}
      >
        <DefaultRenderer navigationState={children[0]} onNavigate={this.props.onNavigate} />
      </Drawer>
    );
  }
}

第二个是近乎空的SideMenu.js

import React from 'react';
import { View, Text } from 'react-native';

const SideMenu = () => (
  <View>
    <Text> 
      SideMenu
    </Text>
  </View>
);

export default SideMenu;

然后我尝试修改添加到mainBucket定义NavigationDrawer组件

的场景定义
<Scene key="mainBucket" component={NavigationDrawer}>
  <Scene key="schedule" initital component={Schedule} title="Impegni" hideNavBar={false} />
  <Scene key="peopleList" component={PeopleList} title="Lista persone" hideNavBar={false} />
</Scene>

问题是我不能再看到带有标题的react-native-router-flux标头,所以我没有更多的顶级操作栏。

我做错了什么??

1 个答案:

答案 0 :(得分:1)

这很有效。我用另一个场景包裹了整个mainBucket,在这个场景中我用抽屉作为组件。

另请注意,我将样式移动到单个场景

const scenes = Actions.create(
  <Scene key="root">

    <Scene key="splashBucket">
      <Scene key="splashScene" component={SplashScreen} title="Splash Screen Title" hideNavBar />
    </Scene>

    <Scene key="authBucket" sceneStyle={{ paddingTop: 65 }}>
      <Scene key="authScene" component={AuthForm} title="Accesso" hideNavBar={false} />
    </Scene>

    <Scene key="mainBucket" component={NavigationDrawer}>
      <Scene key="mainBucketInternal">
        <Scene 
          initital
          key="schedule" 
          component={Schedule} 
          title="Impegni" 
          hideNavBar={false} 
          sceneStyle={{ paddingTop: 65 }} 
        />
        <Scene 
          key="peopleList" 
          component={PeopleList} 
          title="Lista persone" 
          hideNavBar={false} 
          sceneStyle={{ paddingTop: 65 }} 
        />
      </Scene>
    </Scene>

  </Scene>
);

const AppRoutes = () => (
  <Router 
    scenes={scenes}
  />
);

export default AppRoutes;