Commit cdd217083b6f75828df3910f914dc5d9bf46437f
update code to work with new `react-router` + `redux-simple-router`
Michael Williams committed on 1/18/2016, 8:14:39 AMParent: 6c495c8b7fdf2fb19251200cd2cd547883d7d533
Files changed
app/render-browser.js | changed |
app/render.js | changed |
app/store.js | changed |
package.json | changed |
app/render-browser.js | ||
---|---|---|
@@ -1,9 +1,8 @@ | ||
1 | 1 | import React from 'react' |
2 | 2 | import { render } from 'react-dom' |
3 | 3 | import { Provider } from 'react-redux' |
4 | -import { Router } from 'react-router' | |
5 | -import { createHistory } from 'history' | |
4 | +import { browserHistory as history, Router } from 'react-router' | |
6 | 5 | import { syncReduxAndRouter } from 'redux-simple-router' |
7 | 6 | |
8 | 7 | import createRoutes from 'app/routes' |
9 | 8 | import createStore from 'app/store' |
@@ -12,13 +11,10 @@ | ||
12 | 11 | if (process.env.NODE_ENV === 'development') { |
13 | 12 | var DevTools = require('app/util/dev-tools').default |
14 | 13 | } |
15 | 14 | |
16 | -const store = createStore(window.__data) | |
17 | -const history = createHistory() | |
15 | +const store = createStore(window.__data, history) | |
18 | 16 | |
19 | -syncReduxAndRouter(history, store) | |
20 | - | |
21 | 17 | const main = ( |
22 | 18 | <Router createElement={fetchElement(store)} history={history}> |
23 | 19 | { createRoutes(store) } |
24 | 20 | </Router> |
app/render.js | ||
---|---|---|
@@ -4,10 +4,9 @@ | ||
4 | 4 | import Url from 'url' |
5 | 5 | import React from 'react' |
6 | 6 | import { renderToString } from 'react-dom/server' |
7 | 7 | import { Provider } from 'react-redux' |
8 | -import { createHistory } from 'history' | |
9 | -import { Router, RoutingContext, match } from 'react-router' | |
8 | +import { Router, RouterContext, match, createMemoryHistory as createHistory } from 'react-router' | |
10 | 9 | import sendHtml from 'send-data/html' |
11 | 10 | import sendError from 'send-data/error' |
12 | 11 | import redirect from 'predirect' |
13 | 12 | |
@@ -20,9 +19,10 @@ | ||
20 | 19 | |
21 | 20 | return http.createServer(render) |
22 | 21 | |
23 | 22 | function render (req, res) { |
24 | - const store = createStore() | |
23 | + const history = createHistory() | |
24 | + const store = createStore(undefined, history) | |
25 | 25 | |
26 | 26 | match({ |
27 | 27 | routes: createRoutes(store), |
28 | 28 | location: req.url |
@@ -43,9 +43,9 @@ | ||
43 | 43 | renderProps.location, |
44 | 44 | renderProps.params |
45 | 45 | ).then(function () { |
46 | 46 | const component = <Provider store={store}> |
47 | - <RoutingContext { ...renderProps } /> | |
47 | + <RouterContext { ...renderProps } /> | |
48 | 48 | </Provider> |
49 | 49 | |
50 | 50 | var innerHtml |
51 | 51 | try { |
app/store.js | ||
---|---|---|
@@ -1,7 +1,7 @@ | ||
1 | 1 | import { createStore, compose, applyMiddleware } from 'redux' |
2 | 2 | import thunk from 'redux-thunk' |
3 | -import { createHistory } from 'history' | |
3 | +import { syncHistory } from 'redux-simple-router' | |
4 | 4 | |
5 | 5 | import reducer from 'app/reducer' |
6 | 6 | |
7 | 7 | if (process.env.NODE_ENV === 'development') { |
@@ -13,17 +13,11 @@ | ||
13 | 13 | let middleware = [] |
14 | 14 | |
15 | 15 | middleware.push(thunk) |
16 | 16 | |
17 | -storeEnhancers.push( | |
18 | - applyMiddleware(...middleware) | |
19 | -) | |
20 | - | |
21 | 17 | if (process.env.NODE_ENV === 'development') { |
22 | 18 | if (process.browser) { |
23 | - storeEnhancers.push( | |
24 | - applyMiddleware(logger()) | |
25 | - ) | |
19 | + middleware.push(logger()) | |
26 | 20 | } |
27 | 21 | |
28 | 22 | storeEnhancers.push(DevTools.instrument()) |
29 | 23 | |
@@ -36,15 +30,26 @@ | ||
36 | 30 | )) |
37 | 31 | } |
38 | 32 | } |
39 | 33 | |
40 | -const createEnhancedStore = compose( | |
41 | - ...storeEnhancers | |
42 | -)(createStore) | |
34 | +export default function finalCreateStore(initialState, history) { | |
35 | + const historyMiddleware = syncHistory(history) | |
43 | 36 | |
44 | -export default function finalCreateStore(initialState) { | |
37 | + const finalMiddleware = middleware.concat([ | |
38 | + historyMiddleware | |
39 | + ]) | |
40 | + | |
41 | + const createEnhancedStore = compose( | |
42 | + applyMiddleware(...finalMiddleware), | |
43 | + ...storeEnhancers | |
44 | + )(createStore) | |
45 | + | |
45 | 46 | const store = createEnhancedStore(reducer, initialState) |
46 | 47 | |
48 | + if (process.env.NODE_ENV === 'development') { | |
49 | + historyMiddleware.listenForReplays(store) | |
50 | + } | |
51 | + | |
47 | 52 | if (module.hot) { |
48 | 53 | module.hot.accept('app/reducer', () => { |
49 | 54 | store.replaceReducer(require('app/reducer')) |
50 | 55 | }) |
package.json | ||
---|---|---|
@@ -116,9 +116,8 @@ | ||
116 | 116 | "feathers-hooks": "^0.6.0", |
117 | 117 | "feathers-knex": "^2.0.0", |
118 | 118 | "feathers-rest": "^1.1.0", |
119 | 119 | "feathers-tcomb": "^1.0.0", |
120 | - "history": "^1.13.1", | |
121 | 120 | "isomorphic-fetch": "^2.2.1", |
122 | 121 | "knex": "^0.9.0", |
123 | 122 | "lnfs-cli": "^1.0.1", |
124 | 123 | "lodash": "^4.0.0", |
Built with git-ssb-web