未定义的函数(evaluating '_reactNavigation.NavigationActions.reset')

9
我希望能够在特定的时间后将闪屏导航至下一屏。闪屏有一个动画,使用Airbnb Lottie for React Native完成。
闪屏代码如下:

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 />;
  }
}

任何帮助/反馈?
2个回答

20

NavigationActions 中的 reset 动作已被移除,而在 react-navigation v2 中有针对 StackNavigatorStackActions

StackActions 是一个包含方法的对象,用于生成与基于栈的导航器相关的操作。它的方法扩展了 NavigationActions 中可用的操作。

支持以下操作:

Reset - 用新状态替换当前状态

Replace - 用其他路由替换给定键的路由

Push - 在堆栈顶部添加路由,并向前导航到该路由

Pop - 导航回先前的路由

PopToTop - 导航到堆栈的顶部路由,关闭所有其他路由


12
import { StackActions, NavigationActions } from 'react-navigation';

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

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

谢谢,这对我有用,但它没有动画,如何解决? - Ehsan Zargar Ershadi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接