Skip to content

Instantly share code, notes, and snippets.

@borisd
Created December 21, 2017 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borisd/4c72fd5f13cf7cc9b6a590c40dfe177d to your computer and use it in GitHub Desktop.
Save borisd/4c72fd5f13cf7cc9b6a590c40dfe177d to your computer and use it in GitHub Desktop.
React Router 4 - Redirect middleware
import { createBrowserHistory } from 'history'
const history = createBrowserHistory({});
export default history;
import React from 'react';
import ReactDOM from 'react-dom';
import { Router } from 'react-router-dom';
import history from './history';
import App from 'components/App';
ReactDOM.render(
<Provider store={ store }>
<Router history={ history }>
<App/>
</Router>
</Provider>,
document.getElementById('root')
);
import * as actions from 'consts/action-types';
import history from '../history';
const redirects = ({ dispatch, getState }) => next => action => {
next(action);
switch (action.type) {
case actions.LOGIN:
const target = window.sendBack || '/';
window.sendBack = null;
history.push(target);
break;
case actions.LOGOUT:
history.push('/login');
break;
case actions.REDIRECT:
history.push(action.payload);
break;
default:
break;
}
};
export default redirects;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment