Skip to content

Commit 5d6c893

Browse files
committed
Introduce alternative redux-driven history approach
1 parent a668989 commit 5d6c893

File tree

4 files changed

+77
-11
lines changed

4 files changed

+77
-11
lines changed

examples/basic/ReduxRouterHistory.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import uid from 'uid';
2+
import { createMemoryHistory } from 'history';
3+
4+
const CHANGE_LOCATION = 'CHANGE_LOCATION';
5+
6+
export function changeLocation(location) {
7+
return { type: CHANGE_LOCATION, location, id: uid() };
8+
}
9+
10+
export function historyReducer(state = { id: null }, action) {
11+
if(action.type === CHANGE_LOCATION) {
12+
return { id: action.id, location: action.location };
13+
}
14+
return state;
15+
}
16+
17+
export function createReduxRouterHistory(browserHistory, store) {
18+
const memoryHistory = createMemoryHistory();
19+
let lastId = null;
20+
let storeListeningToBrowser = true;
21+
let browserListeningToStore = true;
22+
23+
store.subscribe(() => {
24+
const history = store.getState().history;
25+
26+
if(lastId !== history.id) {
27+
lastId = history.id;
28+
29+
console.log("pushing to memoryHistory from store subscriber");
30+
memoryHistory.push(history.location);
31+
32+
if(browserListeningToStore) {
33+
console.log("pushing to browserHistory from store subscriber");
34+
storeListeningToBrowser = false;
35+
browserHistory.push(history.location);
36+
storeListeningToBrowser = true;
37+
}
38+
}
39+
});
40+
41+
browserHistory.listen((location) => {
42+
if(storeListeningToBrowser) {
43+
console.log("got new location in browserHistory listener, sending to store")
44+
browserListeningToStore = false;
45+
store.dispatch(changeLocation(location));
46+
browserListeningToStore = true;
47+
}
48+
});
49+
50+
const reduxRouterHistory = Object.assign({}, memoryHistory, {
51+
push: function(location) {
52+
console.log("Got push in reduxRouterHistory (i.e. <Link/>). sending to store");
53+
store.dispatch(changeLocation(location));
54+
}
55+
});
56+
57+
return reduxRouterHistory;
58+
59+
}

examples/basic/app.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,33 @@ const { compose, createStore, combineReducers } = require('redux');
44
const { Provider } = require('react-redux');
55
const { Router, Route, IndexRoute } = require('react-router');
66
const createHistory = require('history/lib/createHashHistory');
7-
const { syncReduxAndRouter, routeReducer } = require('redux-simple-router');
7+
// const { syncReduxAndRouter, routeReducer } = require('redux-simple-router');
8+
9+
import { createReduxRouterHistory, historyReducer} from './ReduxRouterHistory';
10+
11+
812
import { devTools } from 'redux-devtools';
913
const { DevTools, DebugPanel, LogMonitor } = require('redux-devtools/lib/react');
1014

1115
const reducers = require('./reducers');
1216
const { App, Home, Foo, Bar } = require('./components');
1317

1418
const reducer = combineReducers(Object.assign({}, reducers, {
15-
routing: routeReducer
19+
history: historyReducer
1620
}));
1721
const finalCreateStore = compose(
1822
devTools()
1923
)(createStore);
2024
const store = finalCreateStore(reducer);
2125
const history = createHistory();
2226

23-
syncReduxAndRouter(history, store);
27+
// syncReduxAndRouter(history, store);
28+
const reduxRouterHistory = createReduxRouterHistory(history, store);
2429

2530
ReactDOM.render(
2631
<Provider store={store}>
2732
<div>
28-
<Router history={history}>
33+
<Router history={reduxRouterHistory}>
2934
<Route path="/" component={App}>
3035
<IndexRoute component={Home}/>
3136
<Route path="foo" component={Foo}/>

examples/basic/components/App.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const React = require('react');
22
const { Link } = require('react-router');
33
const { connect } = require('react-redux');
4-
const { pushPath } = require('redux-simple-router');
4+
// const { pushPath } = require('redux-simple-router');
5+
import { changeLocation } from '../ReduxRouterHistory';
56

6-
function App({ pushPath, children }) {
7+
function App({ changeLocation, children }) {
78
return (
89
<div>
910
<header>
@@ -16,7 +17,7 @@ function App({ pushPath, children }) {
1617
<Link to="/bar">Bar</Link>
1718
</header>
1819
<div>
19-
<button onClick={() => pushPath('/foo')}>Go to /foo</button>
20+
<button onClick={() => changeLocation('/foo')}>Go to /foo</button>
2021
</div>
2122
<div style={{marginTop: '1.5em'}}>{children}</div>
2223
</div>
@@ -25,5 +26,5 @@ function App({ pushPath, children }) {
2526

2627
module.exports = connect(
2728
null,
28-
{ pushPath }
29+
{ changeLocation }
2930
)(App);

examples/basic/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
"name": "rsr-basic-example",
33
"version": "0.0.0",
44
"dependencies": {
5-
"history": "^1.14.0",
5+
"history": "1.17.0",
66
"react": "^0.14.2",
77
"react-dom": "^0.14.2",
88
"react-redux": "^4.0.0",
9-
"react-router": "^1.0.0",
9+
"react-router": "1.0.3",
1010
"redux": "^3.0.4",
11-
"redux-simple-router": "0.0.8"
11+
"redux-simple-router": "0.0.8",
12+
"uid": "0.0.2"
1213
},
1314
"devDependencies": {
1415
"babel-core": "^6.1.21",

0 commit comments

Comments
 (0)