如何使用redux实现react-native-router-flux中的react-native-drawer?

时间:2016-09-23 09:56:24

标签: javascript css reactjs react-native react-native-router-flux

我正在尝试使用redux在react-native-router-flux中实现react-native-drawer。尝试遵循此示例:https://github.com/aksonov/react-native-router-flux/blob/master/docs/OTHER_INFO.md但仍然收到错误:Element type is invalid: expected a string (for built-in components) but got: object. Check the render method of 'DefaultRenderer'.

在react-native-router-flux中实现抽屉的正确方法是什么?

编辑 - 更新:但仍然收到错误'元素类型无效......'

快速注意:如果我做Actions.drawer我得到错误但是如果我做了Actions.home没有任何事情发生但行动REACT_NATIVE_ROUTER_FLUX_RESET仍被调用

const RouterWithRedux = connect()(Router)
const store = configureStore()

export default class App extends Component {
  render() {
    return (
      <Provider store={store}>
        <RouterWithRedux>
          <Scene key='root'>
            <Scene component={Login} initial={true} key='login' title='Login'/>
            <Scene key="drawer" component={NavDrawer} initial={true}>
              <Scene component={Home} key='home' title='Home' type='reset'/>
            </Scene>
          </Scene>
        </RouterWithRedux>
      </Provider>
    )
  }
}

然后我在Login中按下一个按钮,它会触发Actions.导航到。 NavDrawer是:

import React, { PropTypes } from 'react'
import Drawer from 'react-native-drawer'
import { Actions, DefaultRenderer } from 'react-native-router-flux'

import NavDrawerPanel from './NavDrawerPanel'

export default class NavDrawer extends Component {

  componentDidMount() {
      Actions.refresh({key: 'drawer', ref: this.refs.navigation});
  }

  render() {
    const state = this.props.navigationState;
    const children = state.children;

    return (
      <Drawer
        ref="navigation"
        type="displace"
        content={<NavDrawerPanel />}
        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>
    );
  }
}

NavDrawerPanel是:

import React from 'react';
import {PropTypes} from "react";
import {
  StyleSheet,
  Text,
  View,
} from "react-native";
import { Actions } from 'react-native-router-flux';

const NavDrawerPanel = (props, context) => {
  const drawer = context.drawer;
  return (
    <View style={styles.container}>
      <TouchableHighlight>
        <Text>Home</Text>
      </TouchableHighlight>
      <TouchableHighlight>
        <Text>Profile</Text>
      </TouchableHighlight>
      <TouchableHighlight>
        <Text>Friends</Text>
      </TouchableHighlight>
    </View>
  )
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 30,
    backgroundColor: 'black'
  },
})

错误:

enter image description here

2 个答案:

答案 0 :(得分:0)

请参阅this链接...只需在第一个答案中复制并粘贴抽屉组件... 尝试使用此代码并替换为DefaultRenderer:

 <DefaultRenderer navigationState={children[0]} onNavigate={this.props.onNavigate}/>

答案 1 :(得分:0)

我认为问题出在您的组件中。

请注意,您要导入抽屉视图,如下所示:

import NavDrawerPanel from './NavDrawerPanel'

但如果仔细查看NavDrawerPanel文件,则不会出现默认导出。所以没有

const NavDrawerPanel

你应该:

export const NavDrawerPanel

错误有点误导,但它指的是抽屉的视图而不是抽屉的组件本身。如果有诀窍,请告诉我:)。