Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't React Native Drawer being triggered using React Native Router Flux + Redux?

I am using the following components to create a React Native + Redux app:

  • React Native Router Flux
  • React Native Drawer

I tried following exactly the example provided on how to implement the drawer, yet when navigating to where the drawer should be displayed, using Actions.drawer, I get the error:

enter image description here

But if I try to navigate to the Scene, via Actions.home, inside the drawer Scene, nothing happens but the action REACT_NATIVE_ROUTER_FLUX_RESET is still being called via redux-logger.

Tried following the example exactly but no luck. What could I be doing wrong?

Here is my set up for scene using Redux:

// @flow
import React, { Component } from 'react'
import {
  ActionConst,
  Actions,
  Router,
  Scene,
} from 'react-native-router-flux'
import {
  Provider,
  connect,
} from 'react-redux'

import configureStore from './store/configureStore'
import Login from './components/Login'
import Home from './components/Home'
import NavDrawer from './components/NavDrawer'

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}>
              <Scene component={Home} key='home' title='Home' type='reset' initial={true}/>
            </Scene>
          </Scene>
        </RouterWithRedux>
      </Provider>
    )
  }
}

Then I press a button in Login and it triggers Actions. to navigate to.

The NavDrawer is:

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 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>
    );
  }
}

And NavDrawerPanel is:

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 onPress={Actions.home}>
        <Text>Home Page</Text>
      </TouchableHighlight>
      <TouchableHighlight onPress={Actions.login}>
        <Text>Login Page</Text>
      </TouchableHighlight>
    </View>
  )
}

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

EDIT Here are what's being imported where the Scene + Redux is set up:

// @flow
import React, { Component } from 'react'
import {
  ActionConst,
  Actions,
  Router,
  Scene,
} from 'react-native-router-flux'
import {
  Provider,
  connect,
} from 'react-redux'

import configureStore from './store/configureStore'
import Login from './components/Login'
import Home from './components/Home'
import NavDrawer from './components/NavDrawer'

EDIT 2 - console.log(this.props)

Doing console.log(this.props) in component Login: enter image description here

When Actions.home() from component Login: enter image description here

When Actions.drawer() from component Login: enter image description here

EDIT 3 - console.log(this.props) inside NavDrawer.js

The NavDrawer never gets rendered so console.log(this.props) doesn't get logged

But with console.log(this.props) inside NavDrawer.js and Actions.home, I get:

enter image description here

And with console.log(this.props) inside NavDrawer.js and Actions.drawer, I get:

enter image description here

like image 950
Jo Ko Avatar asked Sep 27 '16 17:09

Jo Ko


1 Answers

I'm not an expert in this but I'm using react-native-router-flux and react-native-drawer in my application without any problems. Some of the differences I see between your code and mine are:

  1. You have two scenes set to initial={true}. This might mess up the Router.
  2. I don't navigate directly to the drawer using Actions.drawer but instead I navigate to the home scene using Actions.home.

If nothing of these works, could you share the render() function of your home component?

like image 74
Georgios Avatar answered Oct 25 '22 05:10

Georgios