Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go back 2 pages - React-Router - ReactJS -

I have an component that it goBack to the previous page. I use that: this.props.history.goBack(); but I wanna that go back two levels. I have tried a lot of thing as this.props.history.goBack(-2); but I do not achieve that. I am using ReactRouter v4.

How could I do that? Thank you.

like image 536
JuMoGar Avatar asked Jun 06 '17 01:06

JuMoGar


People also ask

How does react router detect about in react router?

Now according to line 6, if we go to the /about page, React Router first checks if the route contains a /. Since this condition is true, Home is being displayed again. Additionally, since the path contained the about keyword, then the About component is being rendered as well.

How to build a react app with multiple pages or routes?

Photo by Sven Pieren on Unsplash. Traditionally, React has been used to build single-page web apps. However, if you insisted on building a React app with multiple pages or routes, the following process had to be done: Tell React to render the page according to the route that the user has navigated to.

How to render all components of a subroutes in react router?

So in a nutshell, by default, React Router will render all of the components that are subroutes of each other. To rectify this issue, we need to use the Switch component. According to React Router’s documentation, the Switch component essentially stops the whole process of going through all of the route paths as soon as it matches the URL.

How do I navigate back in react-router?

import { useNavigate } from 'react-router-dom'; function goBack() { const navigate = useNavigate(); return <button onClick={() => navigate(-1)}>go back</button> } Share Follow answered yesterday ikuiku 7388 bronze badges


1 Answers

use this.props.history.go(-2)

as it's described in https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/history.md

like image 166
menq Avatar answered Oct 06 '22 18:10

menq