Skip to content

Commit

Permalink
Add the extension to react-counter example
Browse files Browse the repository at this point in the history
  • Loading branch information
zalmoxisus committed May 25, 2016
1 parent ae818c9 commit 1810d2c
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion examples/react-counter/components/Counter.js
@@ -1,5 +1,10 @@
import React, { Component, PropTypes } from 'react'; import React, { Component, PropTypes } from 'react';


const withDevTools = (
// process.env.NODE_ENV === 'development' &&
typeof window !== 'undefined' && window.devToolsExtension
);

const reducer = (state = { counter: 0 }, action) => { const reducer = (state = { counter: 0 }, action) => {
switch (action.type) { switch (action.type) {
case 'INCREMENT': case 'INCREMENT':
Expand All @@ -20,8 +25,16 @@ class Counter extends Component {
this.decrement = this.decrement.bind(this); this.decrement = this.decrement.bind(this);
} }


componentWillMount() {
if (withDevTools) {
this.store = window.devToolsExtension(reducer);
this.store.subscribe(() => { this.setState(this.store.getState()); });
}
}

dispatch(action) { dispatch(action) {
this.setState(reducer(this.state, action)); if (withDevTools) this.store.dispatch(action);
else this.setState(reducer(this.state, action));
} }


increment() { increment() {
Expand Down

0 comments on commit 1810d2c

Please sign in to comment.