I having some little issue migrating from React-Router v3 to v4. in v3 I was able to do this anywhere:
import { browserHistory } from 'react-router';
browserHistory.push('/some/path');
How do I achieve this in v4.
I know that I could use, the hoc withRouter
, react context, or event router props when you are in a Component. but it is not the case for me.
I am looking for the equivalence of NavigatingOutsideOfComponents in v4
This question is related to
reactjs
react-router
If you are using redux and redux-thunk the best solution will be using react-router-redux
// then, in redux actions for example
import { push } from 'react-router-redux'
dispatch(push('/some/path'))
It's important to see the docs to do some configurations.
This works! https://reacttraining.com/react-router/web/api/withRouter
import { withRouter } from 'react-router-dom';
class MyComponent extends React.Component {
render () {
this.props.history;
}
}
withRouter(MyComponent);
Similiary to accepted answer what you could do is use react
and react-router
itself to provide you history
object which you can scope in a file and then export.
history.js
import React from 'react';
import { withRouter } from 'react-router';
// variable which will point to react-router history
let globalHistory = null;
// component which we will mount on top of the app
class Spy extends React.Component {
constructor(props) {
super(props)
globalHistory = props.history;
}
componentDidUpdate() {
globalHistory = this.props.history;
}
render(){
return null;
}
}
export const GlobalHistory = withRouter(Spy);
// export react-router history
export default function getHistory() {
return globalHistory;
}
You later then import Component and mount to initialize history variable:
import { BrowserRouter } from 'react-router-dom';
import { GlobalHistory } from './history';
function render() {
ReactDOM.render(
<BrowserRouter>
<div>
<GlobalHistory />
//.....
</div>
</BrowserRouter>
document.getElementById('app'),
);
}
And then you can just import in your app when it has been mounted:
import getHistory from './history';
export const goToPage = () => (dispatch) => {
dispatch({ type: GO_TO_SUCCESS_PAGE });
getHistory().push('/success'); // at this point component probably has been mounted and we can safely get `history`
};
I even made and npm package that does just that.
In the specific case of react-router
, using context
is a valid case scenario, e.g.
class MyComponent extends React.Component {
props: PropsType;
static contextTypes = {
router: PropTypes.object
};
render () {
this.context.router;
}
}
You can access an instance of the history via the router context, e.g. this.context.router.history
.
Basing on this answer if you need history object only in order to navigate to other component:
import { useHistory } from "react-router-dom";
function HomeButton() {
const history = useHistory();
function handleClick() {
history.push("/home");
}
return (
<button type="button" onClick={handleClick}>
Go home
</button>
);
}
In App.js
import {useHistory } from "react-router-dom";
const TheContext = React.createContext(null);
const App = () => {
const history = useHistory();
<TheContext.Provider value={{ history, user }}>
<Switch>
<Route exact path="/" render={(props) => <Home {...props} />} />
<Route
exact
path="/sign-up"
render={(props) => <SignUp {...props} setUser={setUser} />}
/> ...
Then in a child component :
const Welcome = () => {
const {user, history} = React.useContext(TheContext);
....
Source: Stackoverflow.com