diff --git a/examples/counter/containers/Root.js b/examples/counter/containers/Root.js deleted file mode 100644 index f86fa26aa4..0000000000 --- a/examples/counter/containers/Root.js +++ /dev/null @@ -1,16 +0,0 @@ -import React, { Component } from 'react'; -import { Provider } from 'react-redux'; -import CounterApp from './CounterApp'; -import configureStore from '../store/configureStore'; - -const store = configureStore(); - -export default class Root extends Component { - render() { - return ( - - {() => } - - ); - } -} diff --git a/examples/counter/index.js b/examples/counter/index.js index 6f19211f76..c0449d534e 100644 --- a/examples/counter/index.js +++ b/examples/counter/index.js @@ -1,7 +1,13 @@ import React from 'react'; -import Root from './containers/Root'; +import { Provider } from 'react-redux'; +import CounterApp from './containers/CounterApp'; +import configureStore from './store/configureStore'; + +const store = configureStore(); React.render( - , + + {() => } + , document.getElementById('root') -); +) diff --git a/examples/counter/store/configureStore.js b/examples/counter/store/configureStore.js index b3d4a9bd6b..f47e5ba260 100644 --- a/examples/counter/store/configureStore.js +++ b/examples/counter/store/configureStore.js @@ -1,11 +1,21 @@ -import { createStore, applyMiddleware } from 'redux'; +import { createStore, applyMiddleware, compose } from 'redux'; import thunk from 'redux-thunk'; -import rootReducer from '../reducers'; +import reducer from '../reducers'; +import switchReducer from './switchReducer'; -const createStoreWithMiddleware = applyMiddleware( - thunk -)(createStore); +function onHotReload(useReducer) { + module.hot.accept('../reducers', () => { + const nextReducer = require('../reducers'); + useReducer(nextReducer); + }); +} + +const finalCreateStore = compose( + switchReducer(onHotReload), + applyMiddleware(thunk), + createStore +); export default function configureStore(initialState) { - return createStoreWithMiddleware(rootReducer, initialState); + return finalCreateStore(reducer, initialState); } diff --git a/examples/counter/store/switchReducer.js b/examples/counter/store/switchReducer.js new file mode 100644 index 0000000000..d9482783f6 --- /dev/null +++ b/examples/counter/store/switchReducer.js @@ -0,0 +1,19 @@ +// Note: this is meant to be provided in a separate package. +// I'm only including it here for discussion. + +const SWITCH_REDUCER = { type: 'redux-switch/SWITCH_REDUCER' }; + +export default function switchReducer(onNextReducer) { + return next => (reducer, initialState) => { + let currentReducer = reducer; + let switchingReducer = (state, action) => currentReducer(state, action); + let store = next(switchingReducer, initialState); + + onNextReducer(nextReducer => { + currentReducer = nextReducer; + store.dispatch({ type: SWITCH_REDUCER }); + }); + + return store; + }; +}