Last active
March 21, 2018 09:25
-
-
Save torgeir/4e3aecab117762455dfb4226dc90253f to your computer and use it in GitHub Desktop.
Minimal redux example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const initialState = Immutable.Map({ count: 42 }); | |
const actions = { | |
click: function () { | |
return { type: "click" }; | |
}, | |
asyncClick: function () { | |
return function (dispatch) { | |
setTimeout(() => dispatch(actions.click()), 1000); | |
}; | |
} | |
}; | |
const reducer = function (state = initialState, action) { | |
switch (action.type) { | |
case "click": | |
return state.update("count", c => c + 1); | |
default: | |
return state; | |
} | |
}; | |
const store = redux.createStore(reducer, redux.applyMiddleware(reduxThunk)); | |
const { dispatch, getState, subscribe } = store; | |
const Comp = React.createClass({ | |
render: function () { | |
const { dispatch, count } = this.props; | |
return <div> | |
<button onClick={ () => dispatch(actions.click()) }>+</button> | |
<button onClick={ () => dispatch(actions.asyncClick()) }>async +</button> | |
{ count } | |
</div>; | |
} | |
}); | |
const render = function () { | |
ReactDOM.render( | |
<Comp count={ getState().get("count") } | |
dispatch={ dispatch } />, el); | |
}; | |
subscribe(render); | |
render(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment