fix: Fix router synchronization with redux store
parent
e28c06b85a
commit
a858419f3c
@ -0,0 +1,3 @@
|
|||||||
|
import { createBrowserHistory } from 'history';
|
||||||
|
|
||||||
|
export default createBrowserHistory();
|
||||||
@ -0,0 +1,46 @@
|
|||||||
|
import React, { useLayoutEffect } from 'react';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
import { useDispatch, useSelector } from 'react-redux';
|
||||||
|
import { Router } from 'react-router-dom';
|
||||||
|
|
||||||
|
import { handleLocationChange } from './actions';
|
||||||
|
|
||||||
|
function ReduxRouter({ children, history, selector, basename }) {
|
||||||
|
const state = useSelector(selector);
|
||||||
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
|
useLayoutEffect(() => {
|
||||||
|
const unlisten = history.listen((nextState) => {
|
||||||
|
dispatch(handleLocationChange(nextState.location, nextState.action));
|
||||||
|
});
|
||||||
|
|
||||||
|
dispatch(handleLocationChange(history.location, history.action, true));
|
||||||
|
|
||||||
|
return unlisten;
|
||||||
|
}, [history, dispatch]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Router
|
||||||
|
basename={basename}
|
||||||
|
location={state.location}
|
||||||
|
navigationType={state.action}
|
||||||
|
navigator={history}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</Router>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
ReduxRouter.propTypes = {
|
||||||
|
children: PropTypes.element.isRequired,
|
||||||
|
history: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
|
||||||
|
selector: PropTypes.func,
|
||||||
|
basename: PropTypes.string,
|
||||||
|
};
|
||||||
|
|
||||||
|
ReduxRouter.defaultProps = {
|
||||||
|
selector: ({ router }) => router,
|
||||||
|
basename: undefined,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ReduxRouter;
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
export const LOCATION_CHANGE_HANDLE = '@@router/LOCATION_CHANGE_HANDLE';
|
||||||
|
|
||||||
|
export const handleLocationChange = (location, action, isFirstRendering = false) => ({
|
||||||
|
type: LOCATION_CHANGE_HANDLE,
|
||||||
|
payload: {
|
||||||
|
location,
|
||||||
|
action,
|
||||||
|
isFirstRendering,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const HISTORY_METHOD_CALL = '@@router/HISTORY_METHOD_CALL';
|
||||||
|
|
||||||
|
const createHistoryMethodCaller = (method) => {
|
||||||
|
return (...args) => ({
|
||||||
|
type: HISTORY_METHOD_CALL,
|
||||||
|
payload: {
|
||||||
|
method,
|
||||||
|
args,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const push = createHistoryMethodCaller('push');
|
||||||
|
export const replace = createHistoryMethodCaller('replace');
|
||||||
|
export const go = createHistoryMethodCaller('go');
|
||||||
|
export const back = createHistoryMethodCaller('back');
|
||||||
|
export const forward = createHistoryMethodCaller('forward');
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
import { HISTORY_METHOD_CALL } from './actions';
|
||||||
|
|
||||||
|
const createRouterMiddleware = (history) => {
|
||||||
|
// eslint-disable-next-line consistent-return
|
||||||
|
return () => (next) => (action) => {
|
||||||
|
if (action.type !== HISTORY_METHOD_CALL) {
|
||||||
|
return next(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
payload: { method, args },
|
||||||
|
} = action;
|
||||||
|
|
||||||
|
history[method](...args);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default createRouterMiddleware;
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
import { LOCATION_CHANGE_HANDLE } from './actions';
|
||||||
|
|
||||||
|
const createRouterReducer = (history) => {
|
||||||
|
const initialState = {
|
||||||
|
location: history.location,
|
||||||
|
action: history.action,
|
||||||
|
};
|
||||||
|
|
||||||
|
return (state = initialState, { type, payload } = {}) => {
|
||||||
|
if (type === LOCATION_CHANGE_HANDLE) {
|
||||||
|
const { location, action, isFirstRendering } = payload;
|
||||||
|
|
||||||
|
if (isFirstRendering) {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
location,
|
||||||
|
action,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return state;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default createRouterReducer;
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
export {
|
||||||
|
LOCATION_CHANGE_HANDLE,
|
||||||
|
HISTORY_METHOD_CALL,
|
||||||
|
push,
|
||||||
|
replace,
|
||||||
|
go,
|
||||||
|
back,
|
||||||
|
forward,
|
||||||
|
} from './actions';
|
||||||
|
export { default as createRouterReducer } from './create-router-reducer';
|
||||||
|
export { default as createRouterMiddleware } from './create-router-middleware';
|
||||||
|
export { default as ReduxRouter } from './ReduxRouter';
|
||||||
@ -1,3 +1,5 @@
|
|||||||
import { routerReducer } from '../redux-history-context';
|
import { createRouterReducer } from '../lib/redux-router';
|
||||||
|
|
||||||
export default routerReducer;
|
import history from '../history';
|
||||||
|
|
||||||
|
export default createRouterReducer(history);
|
||||||
|
|||||||
@ -1,6 +0,0 @@
|
|||||||
import { createBrowserHistory } from 'history';
|
|
||||||
import { createReduxHistoryContext } from 'redux-first-history';
|
|
||||||
|
|
||||||
export const { createReduxHistory, routerMiddleware, routerReducer } = createReduxHistoryContext({
|
|
||||||
history: createBrowserHistory(),
|
|
||||||
});
|
|
||||||
@ -1,8 +1,8 @@
|
|||||||
import { takeEvery } from 'redux-saga/effects';
|
import { takeEvery } from 'redux-saga/effects';
|
||||||
import { LOCATION_CHANGE } from 'redux-first-history';
|
import { LOCATION_CHANGE_HANDLE } from '../../../lib/redux-router';
|
||||||
|
|
||||||
import services from '../services';
|
import services from '../services';
|
||||||
|
|
||||||
export default function* routerWatchers() {
|
export default function* routerWatchers() {
|
||||||
yield takeEvery(LOCATION_CHANGE, () => services.handleLocationChange());
|
yield takeEvery(LOCATION_CHANGE_HANDLE, () => services.handleLocationChange());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
import { takeEvery } from 'redux-saga/effects';
|
import { takeEvery } from 'redux-saga/effects';
|
||||||
import { LOCATION_CHANGE } from 'redux-first-history';
|
import { LOCATION_CHANGE_HANDLE } from '../../../lib/redux-router';
|
||||||
|
|
||||||
import services from '../services';
|
import services from '../services';
|
||||||
|
|
||||||
export default function* routerWatchers() {
|
export default function* routerWatchers() {
|
||||||
yield takeEvery(LOCATION_CHANGE, () => services.handleLocationChange());
|
yield takeEvery(LOCATION_CHANGE_HANDLE, () => services.handleLocationChange());
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue