React Native 抽屉导航:如何通过单击特定场景中的自定义按钮打开抽屉?

德里克·杨 |

我试图通过单击特定场景中的以下按钮(屏幕截图)来打开抽屉。当前状态

这是我的代码。

// App.js
import React, { Component } from 'react';
import { createAppContainer, createSwitchNavigator } from 'react-navigation'
import { createStackNavigator } from 'react-navigation-stack'
import { createDrawerNavigator } from 'react-navigation-drawer'
import SplashScreen from 'react-native-splash-screen'
import RNBootSplash from "react-native-bootsplash"

import MainCategory from './src/Scenes/MainCategory'
import SubCategory from './src/Scenes/SubCategory'
import DetailScene from './src/Scenes/DetailScene'
import Questions from './src/Scenes/Questions'
import Ruleta from './src/Scenes/Ruleta'

import CustomDrawerComponent from './src/Components/CustomDrawerComponent'

export default class MainRouter extends Component {
  componentDidMount() {
      SplashScreen.hide()
      RNBootSplash.hide({ duration: 250 })
  }
  render() {
    const StackNavigator = createStackNavigator({
      MainCategory: {screen: MainCategory},
      SubCategory: {screen: SubCategory},
      DetailScene: {screen: DetailScene},
      Questions: {screen: Questions},
      Ruleta: {screen: Ruleta},
    }, {
      initialRouteName: "MainCategory",
      headerMode: "none"
    })

    const DrawerNavigator = createDrawerNavigator({
      MainCategory: {screen: MainCategory},
    }, {
      drawerPosition: 'right',
      contentComponent: CustomDrawerComponent
    })

    const MainNavigator = createSwitchNavigator({
      Stack: StackNavigator,
      Drawer: DrawerNavigator,
    }, {
      initialRouteName: 'Stack',
      contentComponent: CustomDrawerComponent
    })

    const AppContainer = createAppContainer(MainNavigator)
    return(
      <AppContainer />
    )
  }
}
// Specific scene
import React, { Component } from 'react'
import { ScrollView, StatusBar, View, TouchableOpacity, Text, Image, Modal } from 'react-native'

import HeaderBar from '../Components/HeaderBar'

export default class Questions extends Component {
    constructor(props) {
        super(props)
        this.state = {
            headerTitle: props.navigation.getParam('headerTitle'),
            catId: props.navigation.getParam('catId'),
        }
    }

    openSideMenu = () => {
        this.props.navigation.toggleDrawer
        this.props.navigation.openDrawer()
    }

    render() {
        return (
            <View style={rootStyle}>
                <View>
                    <StatusBar hidden={false} translucent backgroundColor="transparent" barStyle="light-content" />
                    <HeaderBar title={headerTitle} onPressLeft={() => this.props.navigation.goBack()} leftShow={true} onPressRight={this.openSideMenu} rightShow={true} />
                </View>
                <ScrollView>
                    <QuestionList todoQues={todoQues} favQues={favQues} action={this.action.bind(this)} />
                </ScrollView>
            </View>
        )
    }
}

但它不起作用。

我希望得到如下屏幕截图所示的结果。预期结果

请让我知道我应该做些什么以及如何在特定场景中使用抽屉导航器。

谢谢你的时间!

柯达

首先,不是调用方法this.props.navigation.toggleDrawer()而是调用方法this.props.navigation.dispatch(DrawerActions.openDrawer())您还需要导入 DrawerActions 才能工作。并删除您在openSideMenu函数中调用的第一个函数,因为您不需要this.props.navigation.openDrawer()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在React Native Android中单击导航抽屉菜单上的2个视图之间的导航?

React-Native中的导航抽屉

如何自定义React Native按钮

React Native createDrawerNavigator无法打开导航抽屉

React Native 抽屉导航屏幕标题和按钮

如何使用React Navigation v.5中的DrawerItemList在抽屉式导航器中传递自定义按钮?

如何仅在特定屏幕(例如主屏幕)上启用抽屉导航 [react native] [react navigation]

为什么自定义函数在屏幕渲染期间在react-native-navigation抽屉组件中自动执行

如何在React Native中使用抽屉导航器?

React Native中每种用户类型的差异抽屉导航器

React Native Navigation:检查抽屉是否打开

打开抽屉时在React Native中隐藏StatusBar

React Native Navigation Drawer-打开页面不在抽屉中

React-Native自定义按钮

无法在 React Native 中的自定义后退按钮上运行反应导航功能

如何在React Native中从自定义导航器导航到createBottomTabNavigator?

React Native 中的自定义单选按钮

Expo:React Native 中 VideoPlayer 上的自定义按钮

从React Native中的自定义按钮更改状态

React Native:禁用Android抽屉

如何在React Native中单击按钮打开DatePicker

如何在 React Native 中自定义材质底部选项卡导航器?

如何在React Native中创建自定义的顶部导航栏

当在react native中的抽屉导航器上单击选项卡时,组件呈现一次

带选项卡导航的react-native抽屉导航

react-native-navigation导航器是未定义的自定义按钮

如何在React Native中向标题添加自定义标题和按钮?

如何在 React Native 中制作自定义形状

React Native-抽屉导航手势起点