【问题标题】:React Native drawer navigation: how to open drawer by clicking custom button in specific scene?React Native 抽屉导航:如何通过单击特定场景中的自定义按钮来打开抽屉?
【发布时间】:2023-03-24 05:43:01
【问题描述】:

我尝试通过在特定场景中单击以下按钮(屏幕截图)来打开抽屉。 current status

这是我的代码。

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

但它不起作用。

我希望得到如下截图所示的结果。 expected result

请让我知道我应该做更多的事情以及如何在特定场景中使用抽屉导航器。

感谢您的宝贵时间!

【问题讨论】:

  • 在我的代码中,this.props.navigation.openDrawer() 不起作用。如果您知道原因,请告诉我为什么它不起作用。
  • 您遇到的任何错误,或者根本无法正常工作
  • 我已经修复了它,但仍然有一些错误。我导入了以下代码,它现在可以工作了。 import { DrawerActions } from 'react-navigation-drawer' ... this.props.navigation.dispatch(DrawerActions.openDrawer()); 现在,抽屉已打开,但在我单击抽屉中的抽屉项目之前完成导航。

标签: javascript react-native react-navigation react-navigation-stack react-navigation-drawer


【解决方案1】:

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

【讨论】:

  • 感谢您的回答。我按照你说的修改了代码,现在 opendrawer 正在工作。我的问题是当抽屉打开时,即使我没有点击任何项目,导航也会自动完成。我检查了代码并尝试删除所有initialRouteName 选项。但是,自动导航已经完成。 (在这种情况下,被导航的路由是drawerNavigator中的第一个路由选项。在上面我的代码中,Maincategory是自动完成的。)
  • 从您的渲染方法中移除您的导航方法(StackNavigator、DrawerNavigator、Switch Navigator)并将它们放在它之外。您的代码应如下所示:export default class AppNavigator extends React.Component { render() { return &lt;AppContainer /&gt;; } }
  • 我按照你说的试过了,但是还是有自动导航的。我将发布另一个关于这个问题的问题。感谢您的友好回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-22
  • 2023-03-08
相关资源
最近更新 更多