Skip to content

Instantly share code, notes, and snippets.

@jihchi
Last active January 29, 2016 15:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jihchi/e831cf5f6f7129e7bff5 to your computer and use it in GitHub Desktop.
Save jihchi/e831cf5f6f7129e7bff5 to your computer and use it in GitHub Desktop.
node_modules/
import createLogger from 'redux-logger';
import PureComponent from 'react-pure-render/component';
import React from 'react';
import ReactDOM from 'react-dom';
import { connect, Provider } from 'react-redux';
import { createStore, combineReducers, applyMiddleware, compose, bindActionCreators } from 'redux';
/**
* Actions
*/
const TICK = 'CLOCK_TICK';
function tick() {
return {
type: TICK,
};
}
/**
* Reducers / Store
*/
function index(state = { now: new Date() }, action) {
switch (action.type) {
case TICK:
return { ...state, now: new Date() };
default:
return state;
}
}
const reducers = combineReducers({
index,
});
const finalCreateStore = compose(
applyMiddleware(createLogger({ collapsed: true }))
)(createStore);
function configureStore(initialState) {
const store = finalCreateStore(reducers, initialState);
if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept('./index.js', () => {
const nextRootReducer = reducers;
store.replaceReducer(nextRootReducer);
});
}
return store;
}
/**
* Components
*/
class Clock extends PureComponent {
render() {
const { value } = this.props;
return <div>{ value.toString() }</div>
}
}
class DumbIndex extends PureComponent {
componentDidMount() {
const { tick } = this.props;
this._intervalID = setInterval(() => tick(), 1000);
}
componentWillUnmount() {
clearInterval(this._intervalID);
}
render() {
const { index } = this.props;
return (
<div>
<h2>Hello worlds!</h2>
<Clock value={ index.now } />
</div>
);
}
}
const Index = connect(
state => ({
index: state.index,
}),
dispatch => bindActionCreators({
tick,
}, dispatch)
)(DumbIndex);
/**
* Entry Point
*/
const store = configureStore();
ReactDOM.render(
<Provider store={ store }>
<Index />
</Provider>,
document.querySelector('#app')
);
{
"name": "simple-react",
"dependencies": {
"react": "^0.14.6",
"react-dom": "^0.14.6",
"react-pure-render": "^1.0.2",
"react-redux": "^4.0.6",
"redux": "^3.0.6",
"redux-logger": "^2.4.0",
"redux-thunk": "^1.0.3"
},
"scripts": {
"start": "heatpack index.js"
},
"devDependencies": {
"react-heatpack": "^3.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment