我希望在反应导航中使用当前路线或屏幕的名称,如果条件需要进行一些更改,我想在其中使用它。
答案 0 :(得分:34)
对于反应导航v5:
import {useRoute} from '@react-navigation/native';
const route = useRoute();
console.log(route.name);
答案 1 :(得分:10)
这在react-navigation v5.x中工作正常
this.props.route.name
答案 2 :(得分:7)
您可以将其捕获为以下代码:
this.props.navigation.state.routeName
答案 3 :(得分:4)
如果您使用的是嵌套导航器,则可以使用此代码获取当前活动屏幕的状态
const getActiveRouteState = function (route: NavigationState): NavigationState {
if (!route.routes || route.routes.length === 0 || route.index >= route.routes.length) {
return route;
}
const childActiveRoute = route.routes[route.index] as NavigationState;
return getActiveRouteState(childActiveRoute);
}
用法:
const activeRoute = getActiveRouteState(this.props.navigation.state);
当我需要从NavigationDrawer获取当前活动屏幕的状态时,我正在使用它。
答案 4 :(得分:4)
使用"react-navigation": "^3.0.8"
和DrawerNavigator时,可以使用this.props对象访问它
this.props.activeItemKey
答案 5 :(得分:4)
const routeNameRef = React.createRef();
<NavigationContainer
ref={navigationRef}
onReady={() => routeNameRef.current = navigationRef.current.getCurrentRoute().name}
onStateChange={() => {
const previousRouteName = routeNameRef.current
const currentRouteName = navigationRef.current.getCurrentRoute().name
if (previousRouteName !== currentRouteName) {
// Do something here with it
}
// Save the current route name for later comparision
routeNameRef.current = currentRouteName
}}
>
{/* ... */}
</NavigationContainer>
);
export function getCurrentRouteName(action) {
return routeNameRef;
}
您可以导入功能getCurrentRouteName并使用它来获取当前路线名称及其在React Navigation 5中任何嵌套导航器中的工作。
答案 6 :(得分:2)
在React Navigation v5中,我可以通过以下方法提取当前路线名称:
flask_login
答案 7 :(得分:1)
如果只想查看当前屏幕是否聚焦,则可以使用navigation.isFocused()。 https://reactnavigation.org/docs/navigation-prop/#isfocused
示例:
float sum = 0, average=0;
float i;
float[] n = new float[5];
for (i = 0; i < 5; i++)
{
Console.Write("State number " + (i + 1) + ":");
// Fix here...
n[(int)i] = float.Parse(Console.ReadLine());
}
for (i = 0; i < 5; i++)
{
// Fix here...
sum += n[(int)i];
average = sum / 5;
}
Console.WriteLine("Sum av all numbers: " + sum);
Console.WriteLine("Average value of all numbers: " + average);
Console.ReadKey();
答案 8 :(得分:1)
this.props.navigation.state.routeName 仅适用于react-navigation 4,但react-navigation 5不支持。
当前路径名称可以通过使用redux来实现:
-Navigator组件将路由对象作为道具传递给子组件
-子组件接收道具,并可以在 route.name
中找到路线名称-要在屏幕更改中获取更新的路线名称,可以在导航上使用 focus 事件监听器
<======实施导航的父组件======>
import React from "react";
import { createMaterialTopTabNavigator } from "@react-navigation/material-top-
tabs";
import ChildScreen from "../screens/Home/childScreen";
const Tab = createMaterialTopTabNavigator();
const ParentNavigations = () => {
return (
<Tab.Navigator
>
<Tab.Screen name="ChildScreen" component={ChildScreen} />
</Tab.Navigator>
);
};
export default ParentNavigations;
<=====子组件=====>
import React, { useEffect } from "react";
import { View, StyleSheet } from "react-native";
import { useDispatch } from "react-redux";
import ActionTypes from "../../store/actions/ActionsTypes";
const ChildScreen = ({ navigation, route }) => {
const dispatch = useDispatch();
useEffect(() => {
const unsubscribe = navigation.addListener("focus", () => {
dispatch({ type: ActionTypes.SETROUTE, payload: route.name }); // every time when screen gets focued it will update the route through redux
});
return unsubscribe;
}, [navigation, route]);
return (
<View style={styles.container}>
<Text>Hello</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#0C0B0B",
},
});
export default ChildScreen;
答案 9 :(得分:1)
我在TabNavigator
中嵌套了多个BottomTabNavigator
。我得到TabNavigator
的当前路线:
const pathAndParams = props.navigation.router.getPathAndParamsForState(props.navigation.state) || {}
const activePath = pathAndParams.path
答案 10 :(得分:0)
对于react-navigation v5,您可以使用useNavigationState挂钩-
import {useNavigationState} from '@react-navigation/native';
const state = useNavigationState(state => state);
const routeName = (state.routeNames[state.index]);
console.log(routeName);
答案 11 :(得分:0)
注册NavigationService
,请参见Navigating without the navigation prop
<App
ref={navigatorRef => {
NavigationService.setTopLevelNavigator(navigatorRef);
}}
/>
递归函数
function getCurrentRoute(nav){
if(Array.isArray(nav.routes)&&nav.routes.length>0){
return getCurrentRoute(nav.routes[nav.index])
}else {
return nav.routeName
}
}
getCurrentRoute(NavigationService.getNavigator().state.nav)
答案 12 :(得分:0)
这对我有用(我在导航抽屉内完成)!
const getCurrentRoute = nav => {
if (Array.isArray(nav.routes) && nav.routes.length > 0) {
return getCurrentRoute(nav.routes[nav.index]);
} else {
return nav.routeName;
}
};
const currentNavigation = getCurrentRoute(this.props.navigation.state);
答案 13 :(得分:0)
对于5.x版本,目前最好的方法是getFocusedRouteNameFromRoute
import { getFocusedRouteNameFromRoute } from '@react-navigation/native';
export default function Stack(route) {
// If the focused route is not found, we need to assume it's the initial screen
// This can happen during if there hasn't been any navigation inside the screen
// In our case, it's "Feed" as that's the first screen inside the navigator
const routeName = getFocusedRouteNameFromRoute(route) ?? 'Feed';
return <> ..... </>
}
答案 14 :(得分:0)
import { useNavigation } from '@react-navigation/native';
const App = () => {
const navigation = useNavigation();
const { dangerouslyGetState } = useNavigation();
const { index, routes } = dangerouslyGetState()
console.log(routes[index].name);
return(
<>
</>
)
};
答案 15 :(得分:0)
这对我有用,请尝试一下。
const getCurrentRouteName = () => {
let _index = props.state.index;
let _routeName = props.state.routeNames;
return _routeName[_index]
}
答案 16 :(得分:0)
下面的“ wix / react-native-navigation”是我的工作解决方案,
import { Navigation } from 'react-native-navigation';
// Create a variable and set the value from navigation events
let navComponent = null
Navigation.events().registerComponentDidAppearListener(event => navComponent = event)
// navComponent will have the following structure
{"componentId": "Component9", "componentName": "HomeScreen", "componentType": "Component", "passProps": {}}
答案 17 :(得分:0)
可以从附加到导航容器的 navigationRef 中获取。其中 navigationRef
是参考。
export const navigationRef = React.createRef()
<NavigationContainer
ref={navigationRef}
>
<Navigator />
</NavigationContainer>
然后使用: const currentRouteName = navigationRef.current.getCurrentRoute().name
或者在功能组件中,您可以使用Ref const navigationRef = React.useRef()
答案 18 :(得分:0)
const Home = ({ navigation, route }) => {
// you will see screen key, name and params
console.log("ROUTE", route);
// rest of your code
};
答案 19 :(得分:0)
您也可以在钩子中使用它。
console.log(navigation.dangerouslyGetState());
答案 20 :(得分:0)
对于本机导航 5.x 使用:
props.state.routeNames[props.state.index]
答案 21 :(得分:0)
这是Justin.Mathew在他的回答中所描述的分步程序。
创建一个名为 RootNavigation.js
的新文件并将以下内容放入其中。
// RootNavigation.js
import * as React from 'react';
export const navigationRef = React.createRef(); // we will access all navigation props by importing this in any of the component
现在从 navigationRef
文件中导入 RootNavigation.js
,并将 NavigationContainer
引用分配给它。在这一步之后,navigationRef
可以作为全局导航道具。
// App.js
import { NavigationContainer } from '@react-navigation/native';
import { navigationRef } from './RootNavigation';
export default function App() {
handleNavigationRef = (ref) => {
// DON'T DO navigationRef = ref, cause this will give you "navigationRef is
// read only" error.
navigationRef.current = ref;
}
return (
<NavigationContainer ref={handleNavigationRef}>
{/* ... */}
</NavigationContainer>
);
}
用法
现在您可以在任何文件中导入 navigationRef
,甚至是嵌套文件。并且可以使用它来获取当前路由和屏幕详细信息。
//SomeNestedComonent.js
import { navigationRef } from "path/to/RootNavigation.js/file";
const route = navigationRef.current?.getCurrentRoute(); //current route object
const currentScreen = route.name; // current screen name