Created
December 21, 2017 08:05
-
-
Save borisd/4c72fd5f13cf7cc9b6a590c40dfe177d to your computer and use it in GitHub Desktop.
React Router 4 - Redirect middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createBrowserHistory } from 'history' | |
const history = createBrowserHistory({}); | |
export default history; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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