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

2

9

I want to navigate a splash screen to next screen after certain timeout. Splash screen have an animation, done with the help of Airbnb Lottie for React Native.

The splashscreen code goes as follows:

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

After I run the app following errors comes up:

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

The Main.js file looks like as follows:

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

Any help/feedback?

Lethargy answered 21/5, 2018 at 6:52 Comment(0)
D
20

reset action is removed from NavigationActions and there is StackActions specific to StackNavigator in v2 of react-navigation.

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

Drove answered 21/5, 2018 at 8:15 Comment(0)
S
12
import { StackActions, NavigationActions } from 'react-navigation';

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

  this.props.navigation.dispatch(navigateAction);
}
Sciamachy answered 14/7, 2018 at 7:50 Comment(1)
Thanks, this worked for me but it has no animation, how to solve that?Apotheosize

© 2022 - 2024 — McMap. All rights reserved.