this.props.navigation.toggedrawer is not a function-React Native 0.59.9 React Navigation 3.11.0

Viewed 3267

I have a requirement of using Redux in Tabnavigator which is inside of an DrawerNavigator.Redux Portion is working fine, But I am unable to open the drawer on click of a button but the drawer is visible on swipe gesture.

I am providing my code:-

App.js:-

import * as React from 'react';
import {createAppContainer,createStackNavigator,createBottomTabNavigator} from 'react-navigation';

import RegisterStack from './routes/Register.route';
import SplashStack from './routes/Splash.route';
import Test from './src/pages/Test';

const AppNavigator = createStackNavigator(
  {
    Splash:  SplashStack,
    Register:  RegisterStack,
    StartTest :Test
  },
  {
    initialRouteName: 'StartTest',
    headerMode:'none'
  });

  let Navigation = createAppContainer(AppNavigator);



  export default class App extends React.Component {
    render() {
      return (
        <Navigation />
      );
    }
  }

code in Test.js:-

import React from 'react';
import { View,StyleSheet,Text,SafeAreaView,Dimensions,Button } from 'react-native';
import {createAppContainer,createMaterialTopTabNavigator,createDrawerNavigator, DrawerActions} from 'react-navigation';
import ReferenceStack from './Reference';
import QuestionStack from './Question';
import { Provider, connect } from 'react-redux';
import { createStore, combineReducers } from 'redux';

function counter(state, action) {
  if (typeof state === 'undefined') {
    return 0;
  }

  switch (action.type) {
    case 'INCREMENT':
      return state + 1;
    case 'DECREMENT':
      return state - 1;
    default:
      return state;
  }
}

let store = createStore(combineReducers({ count: counter }));
class DrawerLayout extends React.Component {
  constructor(props) {
      super(props);
  }

  render() {
      return (
          <SafeAreaView style={styles.container}>
              <Text>{this.props.count}</Text>
              <Button
                title="Increment"
                onPress={() => this.props.dispatch({ type: 'INCREMENT' })}
              />
          </SafeAreaView>
      )
  }
}

const styles = StyleSheet.create({
  container: {
      flex:1,
      backgroundColor: "red",
  }
});
let CounterContainer = connect(state => ({ count: state.count }))(ReferenceStack);
let StaticCounterContainer = connect(state => ({ count: state.count }))(QuestionStack);
let StaticCounterContainerDrawer = connect(state => ({ count: state.count }))(DrawerLayout);


const DrawerStack = createDrawerNavigator({
  Test: {
      screen: createMaterialTopTabNavigator({

          Reference: {
              screen: CounterContainer
          },
          Question:{
              screen: StaticCounterContainer
          }
      },{
          tabBarPosition: 'bottom',
          tabBarOptions:{
              activeTintColor:'#d61822',
              inactiveTintColor:'#5e5e5e',
              pressColor:'#d61822'
          }
      })
  }
},{
  drawerPosition: "left",
  drawerWidth: Dimensions.get('screen').width*.80,
  contentComponent: StaticCounterContainerDrawer
});

let DrawerTabNavigation =  createAppContainer(DrawerStack);

class Test extends React.Component {
  constructor(props) {
    super(props);
    console.log(props)
  }
    render() {
      return (
        <View style={{flex:1}}>
            <View style={{height:50,backgroundColor:'yellow'}}></View>
              <Provider store={store}>
                <DrawerTabNavigation/>
              </Provider>
            <View style={{height:50,backgroundColor:'yellow'}}>
              <Button
                title="Open Drawer"
                onPress={() => this.props.navigation.toggleDrawer()}
              />
            </View>
          </View>
      );
    }
  }
  export default Test;

When ever Open Drawer button is pressed

_this3.props.navigation.toggedrawer is not a function this error is showing.

I am providing the screen and the error also:-

enter image description here

Please hekp me to open the drawer on click of a button. Thanks in advance

2 Answers

this.props.navigation.toggedrawer will work only for the screens which are in DrawerNavigator.

May be try this.

import { DrawerActions } from "react-navigation";
this.props.navigation.dispatch(DrawerActions.toggleDrawer())
Related