Created
February 7, 2017 07:42
-
-
Save gin0606/6b1f2b29cfe27c8dda36cc8fc4218c49 to your computer and use it in GitHub Desktop.
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
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