简体   繁体   English

在一段时间后如何在ReactJS中从一个屏幕切换到另一个屏幕?

[英]How to switch from one screen to another after a certain time in ReactJS?

I'm new to ReactJS and want to implement the following logic: I have an intro screen in my app and after a few seconds I require to swap the intro screen with the home page. 我是ReactJS的新手,想实现以下逻辑:我的应用程序中有一个简介屏幕,几秒钟后,我需要将简介屏幕与主页交换。

How can I do this? 我怎样才能做到这一点?

I have both the intro screen and home page in two separate components and I'm using React Router. 我在两个单独的组件中都有介绍屏幕和主页,并且我正在使用React Router。

I've been thinking of using a SetTimeOut to change my components, but I don't know how to use it between components. 我一直在考虑使用SetTimeOut更改组件,但是我不知道如何在组件之间使用它。

Here is my App.js code: 这是我的App.js代码:

import React, {Component} from 'react';
import './App.css';
import {Route, Switch} from 'react-router-dom';
import Intro from './components/Intro';
import Home from './components/Home';
import Work from './components/Work';
import About from './components/About';
import Carrers from './components/Carrers';
import Contact from './components/Contact';

class App extends Component {
 constructor(){
   super()
 }

 render() {
 return (

   <React.Fragment>
     <Switch>
     <Intro path= "/" exact component={Intro}/>
     <Route path= "/home" exact component={Home} />
     <Route path= "/about" exact component={About} />
     <Route path= "/work" exact component={Work} />
     <Route path= "/carrers" exact component={Carrers} />
     <Route path= "/contact" exact component={Contact} />
     </Switch>
   </React.Fragment>
 );
 }
}


export default App;

Could you please advise me on the right approach? 您能给我建议正确的方法吗?

Your intro component can be like this 您的介绍组件可以像这样

class Intro extends React.Component {


componentDidMount() {
    setTimeout(() => {
      this.props.history.push('/home')
    }, 5000) // render for 5 seconds and then push to home 
  }
  render() {
    return <h1>render for 5 seconds and then push to home</h1>
  }
}

Please refer to this question: Programmatically navigate using react router 请参考此问题: 使用React Router编程方式导航

Basically, you will get history from props and will use it to push to new route. 基本上,您将从道具中获取历史记录,并将其用于新路线。

const { history } = this.props;
history.push('/new-location')

If you don't see it into your props because it's not the Component from your Route, you will need to use withRouter from 'react-router-dom'. 如果由于它不是Route中的组件而不在道具中看到它,则需要使用“ react-router-dom”中的withRouter。

import { withRouter } from 'react-router-dom';
export default withRouter(Component);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 特定时间后从几天切换到几小时 - Switch from days to hours after a certain time 如何将图像从一个部分切换到另一个部分 - How to switch an image from one part to another 我如何使用reactjs中的路由从加载到另一个URL后从一个页面重定向 - How could i use the routing in reactjs to redirect from one page after loading to another url 如何在ReactJS中依次使用多个setState - How to use multiple setState sequentially one after another in ReactJS 我想从firebase-database获取时间之后运行一个计时器,如何使用ReactJS中的setInterval()将时间减少一秒 - I want to run a timer after getting time from firebase-database, how to decease time by one second using setInterval() in ReactJS AngularJs如何从控制器从一个视图切换到另一个视图 - AngularJs how to switch from one view to another from a controller 如何在 ReactJS 和 AG Grid 中一次播放数组中的一个视频 - How to play one video from array at a time in ReactJS & AG Grid 如何在ReactJS中将变量从一页传递到另一页? - How to pass a variable from one page to another in ReactJS? 在 reactjs 中单击时,如何将按钮从一个 div 移动到另一个? - How to move a button from one div to another when clicked in reactjs? 如何通过 ReactJS 中的 function 将数据从一个组件传递到另一个组件? - How to pass data from one component to another via a function in ReactJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM