react-router go back a page how do you configure history?
I think you just need to enable BrowserHistory on your router by intializing it like that : <Router history={new BrowserHistory}>
.
Before that, you should require BrowserHistory
from 'react-router/lib/BrowserHistory'
I hope that helps !
UPDATE : example in ES6
const BrowserHistory = require('react-router/lib/BrowserHistory').default;
const App = React.createClass({
render: () => {
return (
<div><button onClick={BrowserHistory.goBack}>Go Back</button></div>
);
}
});
React.render((
<Router history={BrowserHistory}>
<Route path="/" component={App} />
</Router>
), document.body);
react-router (v4) how to go back?
I think the issue is with binding:
constructor(props){
super(props);
this.goBack = this.goBack.bind(this); // i think you are missing this
}
goBack(){
this.props.history.goBack();
}
.....
<button onClick={this.goBack}>Go Back</button>
As I have assumed before you posted the code:
constructor(props) {
super(props);
this.handleNext = this.handleNext.bind(this);
this.handleBack = this.handleBack.bind(this); // you are missing this line
}
How to go back to previous route in react-router-dom v6
Try this approach
import { useNavigate } from 'react-router-dom';
function YourApp() {
const navigate = useNavigate();
return (
<>
<button onClick={() => navigate(-1)}>go back</button>
</>
);
}
react-router-dom: go back twice in history on go back event
This functionality is required because C2 acts as a
"post-and-redirect". Imagine cases where browser performs a POST
operation, and when you refresh the page it asks you if you want to
resubmit the form. Well that's exactly my case.
In this case you can redirect in the flow to maintain the history stack you desire. In doing so then any back navigation (imperative/declarative from code or browser's back button) results in going back to the correct page you want the user to go back to.
Action | History Stack | Path | Back Location |
---|---|---|---|
initial | ["/"] | "/" | ~ |
PUSH "/c1" | ["/", "/c1"] | "/c2" | "/" |
PUSH "/c1/c2" | ["/c1", "/c1/c2"] | "/c1/c2" | "/c1" |
REPLACE "/c1/c3" | ["/c1", "/c1/c3"] | "/c1/c3" | "/c1" |
POP (1) | ["/c1"] | "/c1" | ~ |
React Router: Go back to specific page
After quite some research, it seems there's no easy way of doing it, discussions in react-router github wouldn't disagree. A workaround seems to be the consensus. Which is a bit raw to my liking, but anything that works is good.
So instead of keeping every sub page under settings with their own route(to be handled by react-router), I have them controlled by state in settings component. And the following event handling is used:
componentDidMount = () => {
window.onpopstate= () => {
if(this.state.currentSubPage !== 'account-settings') {
this.props.history.push('/settings');
} else {
window.onpopstate = null;
}
};
}
react-router-dom how to navigate back (not in history)
The history
object can only navigate to specifically defined paths via a path string with .push
/.replace
, or navigate forward/backward via a delta with .go
.
You can create a "goToParent" handler that reads the current matched location's pathname
and slice off the last path segment and issue a PUSH.
Example:
import { useLocation, useHistory } from "react-router-dom";
...
const { pathname } = useLocation();
const history = useHistory();
const goUpLevel = () => {
const parent = pathname.split("/").slice(0, -1).join("/");
history.push(parent);
}
If you wanted to go two levels up, i.e. from "/dashboard/1/sub-page/7"
to "/dashboard/1"
then use .slice(0, -2)
to slice off the last two segments.
Related Topics
How to Trigger a Click Event on Disabled Elements
How to Get Line Break Within String Interpolation in Angularjs
React Component Not Re-Rendering When State Changes
How to Remove a Json Inside a Json by a Key Using Jquery
Uncheck a Checkbox and Another Checkbox Will Untick
Preventing Viewport Resize of Web Page When Android Soft Keyboard Is Active
Angular 4: Cannot Instantiate Cyclic Dependency! Injectiontoken_Http_Interceptors
Jquery Class on Click Works on Desktop But Not Mobile
How to Get the Contextpath from Javascript, the Right Way
How to Mute Video With JavaScript or Jquery
Show Multiple Divs on Button Click
Looping Through a Json Object Containing More Objects and Arrays
How to Wait for Element to Disappear in Cypress
Jquery Click Events Firing Multiple Times
Change Name of Uploaded File on Client
Ngoninit Not Being Called When Injectable Class Is Instantiated