6

I have a problem that I haven't been able to solve.

In my React native application, I would like to display a welcome screen at the start. Then 5 seconds later just close it, and display another one. Both are 2 entirely different screens, no need to keep the "come back" arrow.

I have been searching for hours, but I haven't found out how to do it.

Here is my code for now:

import Defis from './components/defis' 
import Quote from './components/quote'

export default class Betty extends Component {
    componentDidMount(){
        // Start counting when the page is loaded
        this.timeoutHandle = setTimeout(()=>{
            // Add your logic for the transition
            this.props.navigation.navigate('Defis') // what to push here?
        }, 5000);
    }

    componentWillUnmount(){
        clearTimeout(this.timeoutHandle); 
    }

    render() {
        return (
            <Quote/>
        );
    }
}

Does anybody know how to do it?

I'm not able to use Navigator.push, moreover Navigator seems deprecated.

Bugs
  • 4,456
  • 9
  • 32
  • 40
David Martins
  • 91
  • 1
  • 1
  • 7

6 Answers6

9

Not Using any navigator this can solve your problem

import Defis from './components/defis' 
import Quote from './components/quote'

export default class Betty extends Component {
constructor(props){
 super(props)
 this.state = {
  component : <Quote />
 }
}


componentDidMount(){

     // Start counting when the page is loaded
     this.timeoutHandle = setTimeout(()=>{
          // Add your logic for the transition
          this.setState({ component: <Defis /> })
     }, 5000);
}

componentWillUnmount(){
     clearTimeout(this.timeoutHandle); 
}

render() {
return (
  this.state.component
);
Arnav Yagnik
  • 722
  • 4
  • 14
7

I have done this to show login screen after the splash screen in react-native as follows:

import Login from './Login'; // My next screen
....
....
const {navigate} = this.props.navigation;
setTimeout(() => {
    navigate('Login'); //this.props.navigation.navigate('Login')
}, 5000);  //5000 milliseconds

I have used react-navigation for the navigation purpose.

Rohit Luthra
  • 1,186
  • 14
  • 26
3

I was doing almost the same thing with "react-native-router-flux". Simply render a first screen, in your case the "Quote", and then set in componentDidMount:

  setTimeout(() => {
     Actions.yourNextSceneName()
  }, milliseconds)

Hope this helps.

bra.Scene
  • 462
  • 5
  • 13
0

This worked for me:

import { NavigationActions } from "react-navigation";

componentDidMount(){
    setTimeOut( () => {
        NavigationActions.navigate('login');
    }, 5000 );
}
0

You can do it with using navigator by returning a View with the onLayout prop and adding the setTimeout function to the prop.

0

How to navigate to another screen after timeout in React Native:

So I have created my navigation structure and the respective pages already.

Using functional component in ReactNative, do this this to the componentthat you want navigate from:

function MyPresentScreen( {navigation}, props ){
      setTimeout(() => {
       navigation.navigate('MyTargetScreen');
     }, 2500);
      return(
        <Text>My Present Screen that I will navigate from after some seconds</>
      )
    };

Note that you can customize the timeout as you wish. The above is 2 and half seconds. Credit: Even though it was written with class component, this article was helpful in helping me figure this out:

AnatuGreen
  • 151
  • 1
  • 7