Skip to content

Commit 44a0dc6

Browse files
committed
Merge pull request #667 from rackt/explicitly-use-hmr
Change examples to explicitly use replaceReducer() for hot reloading
2 parents 7f00f29 + 80197ef commit 44a0dc6

File tree

18 files changed

+128
-113
lines changed

18 files changed

+128
-113
lines changed

examples/async/containers/AsyncApp.js examples/async/containers/App.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { selectReddit, fetchPostsIfNeeded, invalidateReddit } from '../actions';
44
import Picker from '../components/Picker';
55
import Posts from '../components/Posts';
66

7-
class AsyncApp extends Component {
7+
class App extends Component {
88
constructor(props) {
99
super(props);
1010
this.handleChange = this.handleChange.bind(this);
@@ -72,7 +72,7 @@ class AsyncApp extends Component {
7272
}
7373
}
7474

75-
AsyncApp.propTypes = {
75+
App.propTypes = {
7676
selectedReddit: PropTypes.string.isRequired,
7777
posts: PropTypes.array.isRequired,
7878
isFetching: PropTypes.bool.isRequired,
@@ -99,4 +99,4 @@ function mapStateToProps(state) {
9999
};
100100
}
101101

102-
export default connect(mapStateToProps)(AsyncApp);
102+
export default connect(mapStateToProps)(App);

examples/async/containers/Root.js

-16
This file was deleted.

examples/async/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import 'babel-core/polyfill';
2-
32
import React from 'react';
4-
import Root from './containers/Root';
3+
import { Provider } from 'react-redux';
4+
import App from './containers/App';
5+
import configureStore from './store/configureStore';
6+
7+
const store = configureStore();
58

69
React.render(
7-
<Root />,
10+
<Provider store={store}>
11+
{() => <App />}
12+
</Provider>,
813
document.getElementById('root')
914
);

examples/async/store/configureStore.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,15 @@ const createStoreWithMiddleware = applyMiddleware(
99
)(createStore);
1010

1111
export default function configureStore(initialState) {
12-
return createStoreWithMiddleware(rootReducer, initialState);
12+
const store = createStoreWithMiddleware(rootReducer, initialState);
13+
14+
if (module.hot) {
15+
// Enable Webpack hot module replacement for reducers
16+
module.hot.accept('../reducers', () => {
17+
const nextRootReducer = require('../reducers');
18+
store.replaceReducer(nextRootReducer);
19+
});
20+
}
21+
22+
return store;
1323
}
File renamed without changes.

examples/counter/containers/Root.js

-16
This file was deleted.

examples/counter/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import React from 'react';
2-
import Root from './containers/Root';
2+
import { Provider } from 'react-redux';
3+
import App from './containers/App';
4+
import configureStore from './store/configureStore';
5+
6+
const store = configureStore();
37

48
React.render(
5-
<Root />,
9+
<Provider store={store}>
10+
{() => <App />}
11+
</Provider>,
612
document.getElementById('root')
713
);
+12-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import { createStore, applyMiddleware } from 'redux';
22
import thunk from 'redux-thunk';
3-
import rootReducer from '../reducers';
3+
import reducer from '../reducers';
44

55
const createStoreWithMiddleware = applyMiddleware(
66
thunk
77
)(createStore);
88

99
export default function configureStore(initialState) {
10-
return createStoreWithMiddleware(rootReducer, initialState);
10+
const store = createStoreWithMiddleware(reducer, initialState);
11+
12+
if (module.hot) {
13+
// Enable Webpack hot module replacement for reducers
14+
module.hot.accept('../reducers', () => {
15+
const nextReducer = require('../reducers');
16+
store.replaceReducer(nextReducer);
17+
});
18+
}
19+
20+
return store;
1121
}

examples/counter/test/containers/CounterApp.spec.js examples/counter/test/containers/App.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import expect from 'expect';
22
import jsdomReact from '../jsdomReact';
33
import React from 'react/addons';
44
import { Provider } from 'react-redux';
5-
import CounterApp from '../../containers/CounterApp';
5+
import App from '../../containers/App';
66
import configureStore from '../../store/configureStore';
77

88
const { TestUtils } = React.addons;
@@ -11,7 +11,7 @@ function setup(initialState) {
1111
const store = configureStore(initialState);
1212
const app = TestUtils.renderIntoDocument(
1313
<Provider store={store}>
14-
{() => <CounterApp />}
14+
{() => <App />}
1515
</Provider>
1616
);
1717
return {

examples/real-world/containers/Root.js

-34
This file was deleted.

examples/real-world/index.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
import 'babel-core/polyfill';
22
import React from 'react';
3-
import Root from './containers/Root';
43
import BrowserHistory from 'react-router/lib/BrowserHistory';
4+
import { Provider } from 'react-redux';
5+
import { Router, Route } from 'react-router';
6+
import configureStore from './store/configureStore';
7+
import App from './containers/App';
8+
import UserPage from './containers/UserPage';
9+
import RepoPage from './containers/RepoPage';
10+
11+
const history = new BrowserHistory();
12+
const store = configureStore();
513

614
React.render(
7-
<Root history={new BrowserHistory()} />,
15+
<Provider store={store}>
16+
{() =>
17+
<Router history={history}>
18+
<Route path="/" component={App}>
19+
<Route path="/:login/:name"
20+
component={RepoPage} />
21+
<Route path="/:login"
22+
component={UserPage} />
23+
</Route>
24+
</Router>
25+
}
26+
</Provider>,
827
document.getElementById('root')
928
);

examples/real-world/reducers/index.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import paginate from './paginate';
44
import { combineReducers } from 'redux';
55

66
// Updates an entity cache in response to any action with response.entities.
7-
export function entities(state = { users: {}, repos: {} }, action) {
7+
function entities(state = { users: {}, repos: {} }, action) {
88
if (action.response && action.response.entities) {
99
return merge({}, state, action.response.entities);
1010
}
@@ -13,7 +13,7 @@ export function entities(state = { users: {}, repos: {} }, action) {
1313
}
1414

1515
// Updates error message to notify about the failed fetches.
16-
export function errorMessage(state = null, action) {
16+
function errorMessage(state = null, action) {
1717
const { type, error } = action;
1818

1919
if (type === ActionTypes.RESET_ERROR_MESSAGE) {
@@ -26,7 +26,7 @@ export function errorMessage(state = null, action) {
2626
}
2727

2828
// Updates the pagination data for different actions.
29-
export const pagination = combineReducers({
29+
const pagination = combineReducers({
3030
starredByUser: paginate({
3131
mapActionToKey: action => action.login,
3232
types: [
@@ -44,3 +44,11 @@ export const pagination = combineReducers({
4444
]
4545
})
4646
});
47+
48+
const rootReducer = combineReducers({
49+
entities,
50+
pagination,
51+
errorMessage
52+
});
53+
54+
export default rootReducer;
+13-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
import { createStore, applyMiddleware, combineReducers } from 'redux';
1+
import { createStore, applyMiddleware } from 'redux';
22
import thunkMiddleware from 'redux-thunk';
33
import apiMiddleware from '../middleware/api';
44
import loggerMiddleware from 'redux-logger';
5-
import * as reducers from '../reducers';
5+
import rootReducer from '../reducers';
66

7-
const reducer = combineReducers(reducers);
87
const createStoreWithMiddleware = applyMiddleware(
98
thunkMiddleware,
109
apiMiddleware,
1110
loggerMiddleware
1211
)(createStore);
1312

14-
// Creates a preconfigured store for this example.
1513
export default function configureStore(initialState) {
16-
return createStoreWithMiddleware(reducer, initialState);
14+
const store = createStoreWithMiddleware(rootReducer, initialState);
15+
16+
if (module.hot) {
17+
// Enable Webpack hot module replacement for reducers
18+
module.hot.accept('../reducers', () => {
19+
const nextRootReducer = require('../reducers');
20+
store.replaceReducer(nextRootReducer);
21+
});
22+
}
23+
24+
return store;
1725
}

examples/todomvc/containers/TodoApp.js examples/todomvc/containers/App.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Header from '../components/Header';
55
import MainSection from '../components/MainSection';
66
import * as TodoActions from '../actions/todos';
77

8-
class TodoApp extends Component {
8+
class App extends Component {
99
render() {
1010
const { todos, dispatch } = this.props;
1111
const actions = bindActionCreators(TodoActions, dispatch);
@@ -19,15 +19,15 @@ class TodoApp extends Component {
1919
}
2020
}
2121

22-
TodoApp.propTypes = {
22+
App.propTypes = {
2323
todos: PropTypes.array.isRequired,
2424
dispatch: PropTypes.func.isRequired
2525
};
2626

27-
function select(state) {
27+
function mapStateToProps(state) {
2828
return {
2929
todos: state.todos
3030
};
3131
}
3232

33-
export default connect(select)(TodoApp);
33+
export default connect(mapStateToProps)(App);

examples/todomvc/containers/Root.js

-17
This file was deleted.

examples/todomvc/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import 'babel-core/polyfill';
22

33
import React from 'react';
4-
import Root from './containers/Root';
4+
import { Provider } from 'react-redux';
5+
import App from './containers/App';
6+
import configureStore from './store/configureStore';
57
import 'todomvc-app-css/index.css';
68

9+
const store = configureStore();
10+
711
React.render(
8-
<Root />,
12+
<Provider store={store}>
13+
{() => <App />}
14+
</Provider>,
915
document.getElementById('root')
1016
);
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { createStore } from 'redux';
2+
import rootReducer from '../reducers';
3+
4+
export default function configureStore(initialState) {
5+
const store = createStore(rootReducer, initialState);
6+
7+
if (module.hot) {
8+
// Enable Webpack hot module replacement for reducers
9+
module.hot.accept('../reducers', () => {
10+
const nextReducer = require('../reducers');
11+
store.replaceReducer(nextReducer);
12+
});
13+
}
14+
15+
return store;
16+
}

examples/universal/store/configureStore.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,15 @@ const createStoreWithMiddleware = applyMiddleware(
77
)(createStore);
88

99
export default function configureStore(initialState) {
10-
return createStoreWithMiddleware(rootReducer, initialState);
10+
const store = createStoreWithMiddleware(rootReducer, initialState);
11+
12+
if (module.hot) {
13+
// Enable Webpack hot module replacement for reducers
14+
module.hot.accept('../reducers', () => {
15+
const nextRootReducer = require('../reducers');
16+
store.replaceReducer(nextRootReducer);
17+
});
18+
}
19+
20+
return store;
1121
}

0 commit comments

Comments
 (0)