简体   繁体   English

this.props.history.push() 跳过中间页

[英]this.props.history.push() skips middle page

I checked out the tutorials and working on a small project related to routers.我查看了教程并从事了一个与路由器相关的小项目。 I have 3 pages in my project right now.namely Login, Verify and Home.我的项目现在有 3 个页面。即登录、验证和主页。 on all the pages i have a submit button like follows.在所有页面上,我都有一个如下所示的提交按钮。

<button type="submit"  onClick= {this.onSubmit()}>next page name</button>

my onSubmit methods on login and verify page are as follows respectively:我在登录和验证页面上的 onSubmit 方法分别如下:

this.props.history.push('/verify');
this.props.history.push('/home');

these are my imports:这些是我的进口:

import { 
BrowserRouter as Router,
Switch,
Route,
NavLink,
useRouteMatch} from "react-router-dom";

When there was no Home page , navigation from login page to verify was very good;没有主页时,从登录页面到验证的导航非常好; but when I added third page ;但是当我添加第三页时 the "Home" page, when I click the submit Button on Login page, it directly takes me to the " Home " Page skipping the " Verify " page. “主页”页面,当我点击登录页面上的提交按钮时,它直接将我带到“主页”页面,跳过“验证”页面。

Please guide me where I'm going wrong!请指导我哪里出错了! Thanks!谢谢!

You have to change the button code to something like this :您必须将按钮代码更改为如下所示:

<button type="submit" onClick= {()=>this.onSubmit()}>next page name</button>

or something like this :或类似的东西:

<button type="submit" onClick= {this.onSubmit}>next page name</button>

because it the onClick gets a callback function and you are calling a function in it instead因为它 onClick 获得了一个回调函数,而您正在调用它中的一个函数

push is a synchronous action so it does load the /verify route, but it changes too quickly to the /home route. push是一个同步操作,因此它确实加载了/verify路由,但它更改为/home路由的速度太快了。 If you want to load the /verify correctly you can redirect to home from within the route, not in the onSubmit method.如果您想正确加载/verify ,您可以从路由内重定向到 home,而不是在onSubmit方法中。

Also, as Mahdi mentions, you shouldn't call the function directly from the onClick .此外,正如 Mahdi 提到的,您不应该直接从onClick调用该函数。 You need to pass a callback:你需要传递一个回调:

<button type="submit"  onClick= {this.onSubmit}>next page name</button>

For more information on this please to refer to this link .有关这方面的更多信息,请参阅此链接

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM