简体   繁体   English

React-router,URL参数和以编程方式重定向

[英]React-router, URL params, and redirect programatically

I'm currently having some trouble using React-router v4, with redirections when using URL parameters. 我目前在使用React-router v4时遇到一些麻烦,并在使用URL参数时进行了重定向。 Here's the route I'm using: 这是我使用的路线:

<Route path="/blog/page/:number" component={Blog} />

I'm using the UI framework Semantic-ui-react and its component Pagination ( https://react.semantic-ui.com/addons/pagination#pagination-example-shorthand ) and I can't render its elements as Links or NavLinks from react-router-dom. 我正在使用UI框架Semantic-ui-react及其组件分页( https://react.semantic-ui.com/addons/pagination#pagination-example-shorthand ),但无法将其元素呈现为Links或来自react-router-dom的NavLink。

Instead, I have to use the event function onPageChange(activePage) , which allow me to redirect to the page clicked. 相反,我必须使用事件函数onPageChange(activePage) ,该函数允许我重定向到单击的页面。 I figured a way to do so, by rendering a <Redirect to={location} push /> component from react-router-dom, and it works quite well except that it requires a component update management as it won't remount the component Blog... 我想出了一种方法,通过从react-router-dom渲染<Redirect to={location} push />组件,它工作得很好,除了它需要组件更新管理,因为它不会重新安装组件。博客...

My issue is, that I can't use browser's (chrome) button to navigate forward and backward, it just modifies the URL and doesn't remount the component. 我的问题是,我无法使用浏览器的(chrome)按钮来回导航,它只能修改URL而不重新安装组件。 As I have no hook to control the browser "previous" and "next" button, I'm wondering how I could fix it. 由于我无权控制浏览器的“上一个”和“下一个”按钮,因此我想知道如何解决该问题。 Thank you. 谢谢。

react-router具有内置的历史记录管理,您可以使用push(new_location)函数进行导航。

I found a way. 我找到了一个方法。 Actually when you push the previous and next button of your browser, if you are using the same component, the life cycle's methods componentWillUpdate () and componentDidUpdate() will be triggered. 实际上,当您按下浏览器的上一个和下一个按钮时,如果您使用的是相同的组件,则将触发生命周期的方法componentWillUpdate()和componentDidUpdate()。 I parse the URL with this.props.history.location, and now, I can make sure that the component will rerender with the right datas when thoses buttons are pushed. 我使用this.props.history.location解析URL,现在,我可以确保按下这些按钮时,组件将使用正确的数据重新呈现。

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

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