patternreactCritical
Detect Route Change with react-router
Viewed 0 times
routereactchangewithdetectrouter
Problem
I have to implement some business logic depending on browsing history.
What I want to do is something like this:
Is there any way to receive a callback from react-router when the URL gets updated?
What I want to do is something like this:
reactRouter.onUrlChange(url => {
this.history.push(url);
});Is there any way to receive a callback from react-router when the URL gets updated?
Solution
You can make use of
You can configure your routes like
index.js
and then in AppContainer.js
From the history docs:
You can listen for changes to the current location using
The location object implements a subset of the window.location
interface, including:
Locations may also have the following properties:
location.state - Some extra state for this location that does not reside in the URL (supported in
in
The action is one of
got to the current URL.
When you are using react-router v3 you can make use of
You can configure and use your routes like
history.listen() function when trying to detect the route change. Considering you are using react-router v4, wrap your component with withRouter HOC to get access to the history prop.history.listen() returns an unlisten function. You'd use this to unregister from listening.You can configure your routes like
index.js
ReactDOM.render(
,
document.getElementById('root')
);and then in AppContainer.js
class App extends Component {
componentWillMount() {
this.unlisten = this.props.history.listen((location, action) => {
console.log("on route change");
});
}
componentWillUnmount() {
this.unlisten();
}
render() {
return (
{this.props.children}
);
}
}
export default withRouter(App);From the history docs:
You can listen for changes to the current location using
history.listen:history.listen((location, action) => {
console.log(`The current URL is ${location.pathname}${location.search}${location.hash}`)
console.log(`The last navigation action was ${action}`)
})The location object implements a subset of the window.location
interface, including:
**location.pathname** - The path of the URL
**location.search** - The URL query string
**location.hash** - The URL hash fragmentLocations may also have the following properties:
location.state - Some extra state for this location that does not reside in the URL (supported in
createBrowserHistory andcreateMemoryHistory)location.key - A unique string representing this location (supportedin
createBrowserHistory and createMemoryHistory)The action is one of
PUSH, REPLACE, or POP depending on how the usergot to the current URL.
When you are using react-router v3 you can make use of
history.listen() from history package as mentioned above or you can also make use browserHistory.listen()You can configure and use your routes like
import {browserHistory} from 'react-router';
class App extends React.Component {
componentDidMount() {
this.unlisten = browserHistory.listen( location => {
console.log('route changes');
});
}
componentWillUnmount() {
this.unlisten();
}
render() {
return (
)
}
}Code Snippets
ReactDOM.render(
<BrowserRouter>
<AppContainer>
<Route exact path="/" Component={...} />
<Route exact path="/Home" Component={...} />
</AppContainer>
</BrowserRouter>,
document.getElementById('root')
);class App extends Component {
componentWillMount() {
this.unlisten = this.props.history.listen((location, action) => {
console.log("on route change");
});
}
componentWillUnmount() {
this.unlisten();
}
render() {
return (
<div>{this.props.children}</div>
);
}
}
export default withRouter(App);history.listen((location, action) => {
console.log(`The current URL is ${location.pathname}${location.search}${location.hash}`)
console.log(`The last navigation action was ${action}`)
})**location.pathname** - The path of the URL
**location.search** - The URL query string
**location.hash** - The URL hash fragmentimport {browserHistory} from 'react-router';
class App extends React.Component {
componentDidMount() {
this.unlisten = browserHistory.listen( location => {
console.log('route changes');
});
}
componentWillUnmount() {
this.unlisten();
}
render() {
return (
<Route path="/" onChange={yourHandler} component={AppContainer}>
<IndexRoute component={StaticContainer} />
<Route path="/a" component={ContainerA} />
<Route path="/b" component={ContainerB} />
</Route>
)
}
}Context
Stack Overflow Q#45373742, score: 185
Revisions (0)
No revisions yet.