简体   繁体   English

如何在反应路由器中以编程方式进行服务器端路由?

[英]how to do server side routing programmatically in react-router?

I am using react-router-dom in my react project.我在我的React项目中使用了react-router-dom

I am trying to programmatically navigate to a new parameter using: history.push("/newparam")我正在尝试使用以下方法以编程方式导航到新参数: history.push("/newparam")

But the problem is navigating like this, isn't refreshing my page.但问题是像这样导航,没有刷新我的页面。 Looks like its doing fake client看起来它在做假客户
side routing.侧路由。 But I want to refresh my browser when I am navigating programmatically.但是我想在以编程方式导航时刷新浏览器。

So how can I do something like a server-side routing using react-router-dom , where the page will refresh when the site will change url?那么我如何使用react-router-dom执行类似服务器端路由的操作,当站点更改 url 时页面将刷新?

When you use react-router the complete routing is handled by the react on client side, So the server is responsible to only load the index of the frontend application, This is a normal behavior in Single page application.当您使用 react-router 时,完整的路由由客户端的 react 处理,因此服务器只负责加载前端应用程序的索引,这是单页应用程序中的正常行为。 We do all the communication with the server using XHR calls.我们使用 XHR 调用与服务器进行所有通信。 Using history.push("/newparam") or <Link route='/newparam' /> is always going to load the page from client side.使用history.push("/newparam")<Link route='/newparam' />总是会从客户端加载页面。

I dont exactly know what your usecase, But if you want the page to reload every time you navigate to a new page, use html <a> tag, This will initiate server side rendering.我不完全知道您的用例是什么,但是如果您希望每次导航到新页面时都重新加载页面,请使用 html <a>标记,这将启动服务器端呈现。 The JS equivalent of this would be window.location.href = '/your-redirect-url';与此等效的 JS 是window.location.href = '/your-redirect-url';

Let me know what exactly is your use-case so that I can help you further让我知道您的用例到底是什么,以便我可以进一步帮助您

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

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