Skip to content

Commit

Permalink
A bare minimum Redux integration
Browse files Browse the repository at this point in the history
  • Loading branch information
koistya committed Nov 5, 2016
1 parent 39dfad6 commit f8f1413
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 1 deletion.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
"query-string": "4.2.3",
"react": "15.3.2",
"react-dom": "15.3.2",
"react-redux": "4.4.5",
"redux": "3.6.0",
"sequelize": "3.24.5",
"serialize-javascript": "1.3.0",
"source-map-support": "0.4.5",
"sqlite3": "3.1.6",
"universal-router": "2.0.0",
Expand Down
3 changes: 3 additions & 0 deletions src/actions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Redux Actions

For more information visit http://redux.js.org/docs/basics/Actions.html
4 changes: 4 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import queryString from 'query-string';
import { createPath } from 'history/PathUtils';
import history from './core/history';
import App from './components/App';
import createStore from './core/createStore';
import { ErrorReporter, deepForceUpdate } from './core/devUtils';

// Global (context) variables that can be easily accessed from any React component
Expand All @@ -28,6 +29,9 @@ const context = {
const removeCss = styles.map(x => x._insertCss());
return () => { removeCss.forEach(f => f()); };
},
// Initialize a new Redux store
// http://redux.js.org/docs/basics/UsageWithReact.html
store: createStore(window.APP_STATE),
};

function updateTag(tagName, keyName, keyValue, attrName, attrValue) {
Expand Down
7 changes: 7 additions & 0 deletions src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ const ContextType = {
// Enables critical path CSS rendering
// https://github.com/kriasoft/isomorphic-style-loader
insertCss: PropTypes.func.isRequired,
// Integrate Redux
// http://redux.js.org/docs/basics/UsageWithReact.html
store: PropTypes.shape({
subscribe: PropTypes.func.isRequired,
dispatch: PropTypes.func.isRequired,
getState: PropTypes.func.isRequired,
}).isRequired,
};

/**
Expand Down
9 changes: 8 additions & 1 deletion src/components/Html.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
*/

import React, { PropTypes } from 'react';
import serialize from 'serialize-javascript';
import { analytics } from '../config';

function Html({ title, description, style, script, chunk, children }) {
function Html({ title, description, style, script, state, chunk, children }) {
return (
<html className="no-js" lang="en">
<head>
Expand All @@ -24,6 +25,11 @@ function Html({ title, description, style, script, chunk, children }) {
</head>
<body>
<div id="app" dangerouslySetInnerHTML={{ __html: children }} />
{state && <script
dangerouslySetInnerHTML={{
__html: `window.APP_STATE=${serialize(state, { isJSON: true })}`,
}}
/>}
{script && <script src={script} />}
{chunk && <script src={chunk} />}
{analytics.google.trackingId &&
Expand All @@ -46,6 +52,7 @@ Html.propTypes = {
description: PropTypes.string.isRequired,
style: PropTypes.string,
script: PropTypes.string,
state: PropTypes.object,
chunk: PropTypes.string,
children: PropTypes.string,
};
Expand Down
13 changes: 13 additions & 0 deletions src/core/createStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* React Starter Kit (https://www.reactstarterkit.com/)
*
* Copyright © 2014-2016 Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/

import { createStore } from 'redux';
import reducers from '../reducers';

export default createStore.bind(undefined, reducers);
3 changes: 3 additions & 0 deletions src/reducers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Redux Reducers

For more information visit http://redux.js.org/docs/basics/Actions.html
16 changes: 16 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* React Starter Kit (https://www.reactstarterkit.com/)
*
* Copyright © 2014-2016 Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/

/* eslint-disable global-require */

import { combineReducers } from 'redux';

export default combineReducers({
user: require('./user').default,
});
15 changes: 15 additions & 0 deletions src/reducers/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* React Starter Kit (https://www.reactstarterkit.com/)
*
* Copyright © 2014-2016 Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/

export default function user(state = {}, action) {
switch (action.type) {
default:
return state;
}
}
7 changes: 7 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import models from './data/models';
import schema from './data/schema';
import routes from './routes';
import assets from './assets'; // eslint-disable-line import/no-unresolved
import createStore from './core/createStore';
import { port, auth } from './config';

const app = express();
Expand Down Expand Up @@ -96,6 +97,11 @@ app.get('*', async (req, res, next) => {
// eslint-disable-next-line no-underscore-dangle
styles.forEach(style => css.add(style._getCss()));
},
// Initialize a new Redux store
// http://redux.js.org/docs/basics/UsageWithReact.html
store: createStore({
user: req.user || null,
}),
};

const route = await UniversalRouter.resolve(routes, {
Expand All @@ -112,6 +118,7 @@ app.get('*', async (req, res, next) => {
data.children = ReactDOM.renderToString(<App context={context}>{route.component}</App>);
data.style = [...css].join('');
data.script = assets.main.js;
data.state = context.store.getState();
data.chunk = assets[route.chunk] && assets[route.chunk].js;
const html = ReactDOM.renderToStaticMarkup(<Html {...data} />);

Expand Down

0 comments on commit f8f1413

Please sign in to comment.