Skip to content

Instantly share code, notes, and snippets.

@gin0606
Created February 7, 2017 07:42
Show Gist options
  • Save gin0606/6b1f2b29cfe27c8dda36cc8fc4218c49 to your computer and use it in GitHub Desktop.
Save gin0606/6b1f2b29cfe27c8dda36cc8fc4218c49 to your computer and use it in GitHub Desktop.
import React from 'react';
import { createStore } from 'redux';
import { Provider, connect } from 'react-redux';
const View = props => (
<div>
<p>{props.count}</p>
<button onClick={props.onClickIncrement}>+1</button>
<button onClick={props.onClickDecrement}>-1</button>
</div>
);
const mapStateToProps = state => ({
count: state.count,
});
const mapDispatchToProps = dispatch => ({
onClickIncrement: () => {
dispatch({
type: 'increment',
});
},
onClickDecrement: () => {
dispatch({
type: 'decrement',
});
},
});
const Container = connect(mapStateToProps, mapDispatchToProps)(View);
const reducer = (state, action) => {
switch (action.type) {
case 'increment':
return { count: state.count + 1 };
case 'decrement':
return { count: state.count - 1 };
default:
return state;
}
};
const initialState = { count: 0 };
const store = createStore(reducer, initialState);
export default () => (
<Provider store={store}>
<Container />
</Provider>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment