首页 > 解决方案 > React Native Navigation:检查抽屉是否打开

问题描述

我有一个位于 Drawer 标记之外的组件,但它位于 NavigationContainer 内。所以我用useReffrom react 来获取navigate方法。

所以我用这个例子:导航没有导航道具

但是现在,我无法获得抽屉的状态(它是打开还是关闭)。

这是我的应用程序组件:

import 'react-native-gesture-handler';
import React from 'react';
import { createStore, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import { Provider } from "react-redux";
import GetInformationByLocation from "./reducers/GetInformationByLocation";
import Wrapper from './components/Wrapper';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';
import GeoDisplay from "./components/GeoDisplay";
import { Text } from "react-native";
import { navigationRef } from "./helpers/Navigator";

const store = createStore(GetInformationByLocation, applyMiddleware(thunk));
/*
 * TODO:
 * 1. Wrap everything with parent component
 * 2. Search bar
 * 3. Save searched cities.
 * 4. Check if the city is undefined and suggest nearest city.
 * 5. Animated background
 * 6. Fancy and animated font
 * 7. Setup menu преди показване на приложението
 */

const Drawer = createDrawerNavigator();

const App = () => {
    return (
        <Provider store={store}>
            <NavigationContainer ref={navigationRef}>
                <Wrapper />
                <Drawer.Navigator initialRouteName="Home">
                    <Drawer.Screen name="Home" component={GeoDisplay} />
                </Drawer.Navigator>
            </NavigationContainer>
        </Provider >
    );
}


export default App;

和我的 Wrapper.js 组件:

import React, { useEffect, useState } from 'react';
import { StyleSheet, View, StatusBar } from 'react-native';
import { SearchBar, Header } from 'react-native-elements';
import { MainUI } from '../styling/UI';
import * as Navigator from "../helpers/Navigator";
import { DrawerActions } from "@react-navigation/native";

const Wrapper = (props) => {

    const [search, setSearch] = useState(true);

    const openDrawer = () => {
        Navigator.navigationRef.current.dispatch(DrawerActions.openDrawer())
        setSearch(false);
    }


    useEffect(() => {
    }, []);

    return (
        <>
            <StatusBar backgroundColor={"#011E25"} />
            <Header
                leftComponent={{ icon: 'menu', color: '#fff', onPress: () => { openDrawer() } }}
                centerComponent={{ text: 'COVID Bulgaria', style: { color: '#fff' } }}
                rightComponent={{ icon: 'home', color: '#fff' }}
                backgroundColor={'#011E25'}
            />
            {search &&
                <View>
                    <SearchBar placeholder="Търси град" containerStyle={MainUI.searchContainer} />
                </View>
            }
        </>
    )

}

export default Wrapper;

我使用 navigationRef 调度openDrawer()操作, Navigator.navigationRef.current.dispatch(DrawerActions.openDrawer()) 但无法获取抽屉的状态。

我尝试了很多方法,但没有奏效。

提前致谢。

标签: reactjsreact-nativereact-navigation-v5react-navigation-drawer

解决方案


您可以通过获取导航状态来检查抽屉是否打开:

const state = navigationRef.current.getRootState();
const isDrawerOpen = state.history.some((it) => it.type === 'drawer');

上面的代码假设抽屉位于根目录。如果它是嵌套的,则需要遍历状态以找到带有type: 'drawer'.

目前尚不清楚为什么您需要从问题中检查它。通常你不需要检查它。如果您DrawerActions.openDrawer()在抽屉已经打开的情况下分派,则不会发生任何事情。所以检查是不必要的。如果您想在抽屉打开的情况下关闭它,您可以DrawerActions.toggleDrawer()改为调度。


推荐阅读