GVKun编程网logo

react-navigation 路由级登录拦截(reactnative路由)

17

如果您想了解react-navigation路由级登录拦截的相关知识,那么本文是一篇不可错过的文章,我们将对reactnative路由进行全面详尽的解释,并且为您提供关于@react-navigati

如果您想了解react-navigation 路由级登录拦截的相关知识,那么本文是一篇不可错过的文章,我们将对reactnative路由进行全面详尽的解释,并且为您提供关于@ react-navigation / native和@ react-navigation / stack读取整个堆栈、@react-navigation/native 和 @react-navigation/stack 应该是我模块的 peerDependencies 或依赖项吗?、javascript – undefined不是函数(评估’_reactNavigation.NavigationActions.reset’)、React Native Navigation V5 Stack、BottomTab 和 Drawer Navigation的有价值的信息。

本文目录一览:

react-navigation 路由级登录拦截(reactnative路由)

react-navigation 路由级登录拦截(reactnative路由)

// 路由栈

const Navigator = StackNavigator({ Setting: {screen: SettingSCreenView},.... });

1、设置路由状态改变拦截

function getCurrentRouteName(navigationState) {
    if (!navigationState) {
        return null;
    }
    const route = navigationState.routes[navigationState.index];
    // dive into nested navigators
    if (route.routes) {
        return getCurrentRouteName(route);
    }
    return route.routeName;
}


class StartScreen extends Component {
    render() {
        const prefix = Platform.OS === 'android' ? 'xx://aa/' : 'xx://';
        return (
            <Navigator uriPrefix={prefix}
                onNavigationStateChange={
                    (prevstate,currentState) => {
                        const currentScene = getCurrentRouteName(currentState);
                        if(currentScene == 'Setting'){
                        // to do something
                    }
                } 
            />);
    }

2、登录跳转拦截

// 拦截登录的路由
const needLoginRoute = ['Setting'];

const defaultGetStateForAction = StartScreen.router.getStateForAction;

StartScreen.router.getStateForAction = (action,state) => {
    let routeName = dealMsgDeal(action.routeName);
    // 拦截需要登录后跳转的也没
    if(action.routeName === routeName && islogin == false){
        this.routes = [
            ...state.routes,{key: 'id-'+Date.Now(),routeName: 'Login',params: { name: routeName,params: action.params}},];
        return {
            ...state,routes,index: this.routes.length - 1,};
    }
    return defaultGetStateForAction(action,state);
};


// 需要拦截登录的页面
function dealMsgDeal(routeName){
    let theRouteName = '';
    if(routeName){
        for (var i in needLoginRoute) {
            if (needLoginRoute[i] == routeName) {
                theRouteName = routeName;
                break;
            }
        }
    }
    return theRouteName;
}

@ react-navigation / native和@ react-navigation / stack读取整个堆栈

@ react-navigation / native和@ react-navigation / stack读取整个堆栈

您可以使用如下所示的useNavigationState钩子来获取当前状态

https://reactnavigation.org/docs/use-navigation-state/

import {useNavigationState} from '@react-navigation/native';

const state = useNavigationState((state) => state);

alert(JSON.stringify(state.routes));

这将在路线中显示屏幕

@react-navigation/native 和 @react-navigation/stack 应该是我模块的 peerDependencies 或依赖项吗?

@react-navigation/native 和 @react-navigation/stack 应该是我模块的 peerDependencies 或依赖项吗?

如何解决@react-navigation/native 和 @react-navigation/stack 应该是我模块的 peerDependencies 或依赖项吗??

我正在编写一个使用以下依赖项的模块:

{
    "@react-native-community/masked-view": "^0.1.10","@react-navigation/native": "^5.9.4","@react-navigation/stack": "^5.14.4","@react-native-community/masked-view": "^0.1.10","env-cmd": "^10.1.0","firebase": "^8.4.1","react-native-gesture-handler": "^1.10.3","react-native-reanimated": "^2.1.0","react-native-safe-area-context": "^3.2.0","react-native-screens": "^3.1.1"
}

我知道存储单例状态的依赖项不应该在 dependencies 中,而应该在 devDependenciespeerDependencies 中。

我不认为 react-navigation 是一个单独的依赖项,我们可以创建导航的 ref。

可以在我的分布式模块 dependencies 中将这些依赖项用作 package.json 吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

javascript – undefined不是函数(评估’_reactNavigation.NavigationActions.reset’)

javascript – undefined不是函数(评估’_reactNavigation.NavigationActions.reset’)

我想在特定超时后将启动画面导航到下一个屏幕.启动画面有一个动画,在Airbnb Lottie的帮助下为React Native完成.

splashscreen代码如下:

import React from "react";
import { Animated,Easing } from "react-native";
import LottieView from "lottie-react-native";
import { NavigationActions } from "react-navigation";

export default class SplashScreen extends React.Component {
  static navigationoptions = {
    header: null
  };

  constructor() {
    super();
    this.state = {
      progress: new Animated.Value(0),}
  }

  componentDidMount() {
    setTimeout(() => {
      this.navigatetoWalkthrough()
    },3500);
    
    Animated.timing(this.state.progress,{
      tovalue: 1,duration: 3000,easing: Easing.linear,}).start();
  }

  navigatetoWalkthrough = () => {
    const navigateAction = NavigationActions.reset({
      index: 0,actions: [NavigationActions.navigate({ routeName: "Walkthrough" })],});

    this.props.navigation.dispatch(navigateAction);
  }

  render() {
    return(
      <LottieView 
      source={require("../assets/splash/SplashScreenAnimation.json")}
      progress={this.state.progress}
      />
    );
  }
}

我运行应用程序后出现错误:

undefined is not a function (evaluating'_reactNavigation.NavigationActions.reset')

Main.js文件如下所示:

import React from "react";
import { View,Text } from "react-native";
import { createStackNavigator } from "react-navigation";

import SplashScreen from "./screens/SplashScreen";
import Walkthrough from "./screens/Walkthrough";

const Routes = createStackNavigator({
  Home: {
    screen: SplashScreen
  },Walkthrough: {
    screen: Walkthrough
  }
});

export default class Main extends React.Component {
  render() {
    return <Routes />;
  }
}

任何帮助/反馈?

解决方法

从NavigationActions中删除了重置操作,并且在反应导航的v2中有特定于StackNavigator的 StackActions.

StackActions is an object containing methods for generating actions
specific to stack-based navigators. Its methods expand upon the
actions available in NavigationActions.

The following actions are supported:

Reset – Replace current state with a new state

Replace – Replace a route at a given key with another route

Push – Add a route on the top of the stack,and navigate forward to it

Pop – Navigate back to prevIoUs routes

PopToTop – Navigate to the top route of the stack,dismissing all other routes

React Native Navigation V5 Stack、BottomTab 和 Drawer Navigation

如何解决React Native Navigation V5 Stack、BottomTab 和 Drawer Navigation?

我在我的项目中使用了底部、堆栈和抽屉导航。

我需要导航到事件页面而不添加到底部导航器。

我的问题:

如果我需要将一个组件导航到另一个组件而不将组件添加到底部导航器,这里给出了两个文件的代码 如果需要导航到事件页面,如果我在 Component 我可以导航到 Events Compnents。但它也在底部选项卡导航器中显示我不希望底部导航器中的这个组件。

App.js

import ''react-native-gesture-handler'';
    import { StatusBar } from ''expo-status-bar'';
    import React from ''react'';
    import { StyleSheet,Text,View } from ''react-native'';
    import { NavigationContainer } from ''@react-navigation/native'';
    import { createDrawerNavigator } from ''@react-navigation/drawer'';
    import {DrawerContent} from ''./Screens/DrawerContent'';
    import {Events} from ''./Screens/Events''
    import MainTabScreen from ''./Screens/Bottomtab''
    
    const Drawer = createDrawerNavigator();
    
    
    
    export default function App() {
      return (
        <NavigationContainer>
          <Drawer.Navigator drawerContent={props => <DrawerContent {...props} />}>
            <Drawer.Screen name="Home" component={MainTabScreen} />
          </Drawer.Navigator>
        </NavigationContainer>
      );
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,backgroundColor: ''#fff'',alignItems: ''center'',justifyContent: ''center'',},});```
    
    BottomTab Screen.js
    
        enter code here
    
    ```
    import React from "react";
    import { createStackNavigator } from "@react-navigation/stack";
    import Icon from "react-native-vector-icons/Ionicons";
    import { createMaterialBottomTabNavigator } from "@react-navigation/material-bottom-tabs";
    
    import Home from "./HomeScreen";
    import About from "./AboutUs";
    import Contact from "./ContactUs";
    import Test from "./Test";
    import Events from "./Events";
    
    const HomeStack = createStackNavigator();
    const AboutStack = createStackNavigator();
    const ContactStack = createStackNavigator();
    const TestStack = createStackNavigator();
    const Eventsstack = createStackNavigator();
    
    const Tab = createMaterialBottomTabNavigator();
    
    const MainTabScreen = () => (
      <Tab.Navigator
        initialRouteName="Home"
        activeColor="#fff"
        //   style={{ backgroundColor: ''tomato'' }}
      >
        <Tab.Screen
          name="Home"
          component={HomeStackScreen}
          options={{
            tabBarLabel: "Home",tabBarColor: "#009387",tabBarIcon: ({ color }) => (
              <Icon name="ios-home" color={color} size={26} />
            ),}}
        />
        <Tab.Screen
          name="ContactUs"
          component={ContactStackScreen}
          options={{
            tabBarLabel: "Notifications",tabBarColor: "#1f65ff",tabBarIcon: ({ color }) => (
              <Icon name="ios-notifications" color={color} size={26} />
            ),}}
        />
        <Tab.Screen
          name="About"
          component={AboutStackScreen}
          options={{
            tabBarLabel: "Profile",tabBarColor: "#694fad",tabBarIcon: ({ color }) => (
              <Icon name="ios-person" color={color} size={26} />
            ),}}
        />
        <Tab.Screen
          name="Test"
          component={TestStackScreen}
          options={{
            tabBarLabel: "Search",tabBarColor: "#d02860",tabBarIcon: ({ color }) => (
              <Icon name="ios-aperture" color={color} size={26} />
            ),}}
        />
    
        {/* <Tab.Screen
          name="Events"
          component={EventsstackScreen}
          options={{
            tabBarLabel: "Search",}}
        /> */}
      </Tab.Navigator>
    );
    export default MainTabScreen;
    
    const HomeStackScreen = ({ navigation }) => (
      <HomeStack.Navigator
        screenoptions={{
          headerStyle: {
            backgroundColor: "#009387",headerTintColor: "#fff",headerTitleStyle: {
            fontWeight: "bold",}}
      >
        <HomeStack.Screen
          name="Home"
          component={Home}
          options={{
            title: "Home",headerLeft: () => (
              <Icon.Button
                name="ios-menu"
                size={25}
                backgroundColor="#009387"
                onPress={() => navigation.openDrawer()}
              ></Icon.Button>
            ),}}
        />
      </HomeStack.Navigator>
    );
    
    const ContactStackScreen = ({ navigation }) => (
      <ContactStack.Navigator
        screenoptions={{
          headerStyle: {
            backgroundColor: "#1f65ff",}}
      >
        <ContactStack.Screen
          name="ContactUs"
          component={Contact}
          options={{
            title: "ContactUs",headerLeft: () => (
              <Icon.Button
                name="ios-menu"
                size={25}
                backgroundColor="#1f65ff"
                onPress={() => navigation.openDrawer()}
              ></Icon.Button>
            ),}}
        />
      </ContactStack.Navigator>
    );
    const AboutStackScreen = ({ navigation }) => (
      <AboutStack.Navigator
        screenoptions={{
          headerStyle: {
            backgroundColor: "#694fad",}}
      >
        <AboutStack.Screen
          name="About"
          component={About}
          options={{
            title: "AboutUs",headerLeft: () => (
              <Icon.Button
                name="ios-menu"
                size={25}
                backgroundColor="#694fad"
                onPress={() => navigation.openDrawer()}
              ></Icon.Button>
            ),}}
        />
      </AboutStack.Navigator>
    );
    const TestStackScreen = ({ navigation }) => (
      <TestStack.Navigator
        screenoptions={{
          headerStyle: {
            backgroundColor: "#d02860",}}
      >
        <TestStack.Screen
          name="Test"
          component={Test}
          options={{
            title: "Test",headerLeft: () => (
              <Icon.Button
                name="ios-menu"
                size={25}
                backgroundColor="#d02860"
                onPress={() => navigation.openDrawer()}
              ></Icon.Button>
            ),}}
        />
      </TestStack.Navigator>
    );
    const EventsstackScreen = ({ navigation }) => (
      <Eventsstack.Navigator screenoptions={{
          headerStyle: {
            backgroundColor: "#d02860",}}
      >
        <Eventsstack.Screen
          name="Events"
          component={Events}
          options={{
            title: "Events",}}
        />
      </Eventsstack.Navigator>
    );

这里给出了两个文件的代码如果需要导航到事件页面,如果我在 组件中声明事件组件,我将无法导航我可以导航到事件组件。但它也显示在底部选项卡中导航器我不想在底部导航器中使用这个组件。

我需要调用 EventsstackScreen 而不添加到

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

今天关于react-navigation 路由级登录拦截reactnative路由的分享就到这里,希望大家有所收获,若想了解更多关于@ react-navigation / native和@ react-navigation / stack读取整个堆栈、@react-navigation/native 和 @react-navigation/stack 应该是我模块的 peerDependencies 或依赖项吗?、javascript – undefined不是函数(评估’_reactNavigation.NavigationActions.reset’)、React Native Navigation V5 Stack、BottomTab 和 Drawer Navigation等相关知识,可以在本站进行查询。

本文标签: