reactjs - React Native Navigation:检查抽屉是否打开
问题描述
我有一个位于 Drawer 标记之外的组件,但它位于 NavigationContainer 内。所以我用useRef
from 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())
但无法获取抽屉的状态。
我尝试了很多方法,但没有奏效。
提前致谢。
解决方案
您可以通过获取导航状态来检查抽屉是否打开:
const state = navigationRef.current.getRootState();
const isDrawerOpen = state.history.some((it) => it.type === 'drawer');
上面的代码假设抽屉位于根目录。如果它是嵌套的,则需要遍历状态以找到带有type: 'drawer'
.
目前尚不清楚为什么您需要从问题中检查它。通常你不需要检查它。如果您DrawerActions.openDrawer()
在抽屉已经打开的情况下分派,则不会发生任何事情。所以检查是不必要的。如果您想在抽屉打开的情况下关闭它,您可以DrawerActions.toggleDrawer()
改为调度。
推荐阅读
- json - 如何将结构反序列化/序列化为没有键的数组
- setlocale - 我是第一次使用 DeepLabCut。提取帧后,我无法加载这些帧进行标记。显示以下错误
- network-programming - 如何在没有 FCS 的情况下发送以太网帧?
- android - 不同项目/分支中的 Android Instant App
- docker - 为什么我在运行 docker ps 命令时出现错误
- sql - 根据 postgresQL 中的时间戳将值从一个表映射到另一个表
- android - 如何更改我的 android 应用程序的内容
- c# - 查询表达式中等效的 C# LINQ zip 函数
- javascript - 年龄金字塔:3.5.1 chartjs
- java - junit core java反向字符串测试用例