React-router V4 Dispatch Redux Action On Page Change
I would like an action to be dispatched and then intercepted by the reducers on every react-router page change. My use case is: I have the page state persisted in redux store. The
Solution 1:
You could create your history
instance separately, and listen to that and dispatch an action when something changes.
Example
// history.js
import createHistory from 'history/createBrowserHistory';
const history = createHistory();
history.listen(location => {
// Dispatch action depending on location...
});
export default history;
// App.js
import { Router, Route } from 'react-router-dom';
import Home from './components/Home';
import Login from './components/Login';
import history from './history';
class App extends React.Component {
render() {
return (
<Router history={history}>
<div>
<Route exact path="/" component={Home} />
<Route path="/login" component={Login} />
</div>
</Router>
)
}
}
Solution 2:
v4 has the location
object which could be used to check if the app has navigated away from the current page.
Post a Comment for "React-router V4 Dispatch Redux Action On Page Change"