window.location.href 与 react-router-dom 的重定向实用程序 [英] window.location.href vs redirect utility of react-router-dom

查看:63
本文介绍了window.location.href 与 react-router-dom 的重定向实用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 React 和 React Router 编写了 webapp.

I have webapp written using React and React Router.

使用 window.location.href 重定向与使用通过 react-router 提供的组件相比有什么优缺点吗?

Are there any pros/cons of using window.location.href to redirect vs using the component provided via react-router?

推荐答案

如果您不想重新加载浏览器窗口,请使用 history.push('/page-name'),或者window.location.href 如果你想重新加载它.通常在使用 React Router 时,默认行为是使用 history.push (this.props.history.push('/page-name') 更具体,作为导航的其余部分应用程序已经在使用 React Route 的 ,它具有相同的效果.

Use history.push('/page-name') if you don't want the browser window to be reloaded, or window.location.href if you want to reload it. Usually when using the React Router the default behavior is using the history.push (this.props.history.push('/page-name') to be more specific, as the rest of the navigation through the application is already using React Route's <Link>, which has the same effect.

这篇关于window.location.href 与 react-router-dom 的重定向实用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆