我有一个组件位于 Drawer 标签之外,但位于 NavigationContainer 内。 所以我使用了 React 中的
useRef
来获取 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()
来代替。
您可以创建一个辅助类,例如:
class DrawerHandler() {
openDrawer() {
this.drawerOpened = true;
}
closeDrawer() {
this.drawerOpened = false;
}
getDrawerStatus() {
return this.drawerOpened;
}
}
export default new DrawerHandler();
使用打开和关闭功能来打开和关闭抽屉,并使用获取功能来获取抽屉的状态
如果您想确定抽屉是打开还是关闭,您可以执行以下操作:
从'@react-navigation/drawer'导入{useDrawerStatus};
//...
const isDrawerOpen = useDrawerStatus() === '打开';
您可以在开头语句后添加变量。
opendrwr=0;//initial value
<DrawerLayoutAndroid
ref={drawer}
onDrawerClose={()=>{opendrwr=0} }
onDrawerOpen= {()=>{opendrwr=1} }