Created
October 18, 2017 17:43
-
-
Save oamaok/cb459c26dd4983aa305a73ec6e36e24e 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 ReactDOM from 'react-dom'; | |
import { createStore, combineReducers } from 'redux'; | |
import { Provider, connect } from 'react-redux'; | |
function increaseBy(amount) { | |
return { | |
type: 'INCREMENT', | |
amount, | |
}; | |
} | |
function decreaseBy(amount) { | |
return { | |
type: 'DECREMENT', | |
amount, | |
}; | |
} | |
const initialState = 0; | |
function myReducer(state = initialState, action) { | |
switch(action.type) { | |
case 'INCREMENT': | |
return state + action.amount; | |
case 'DECREMENT': | |
return state - action.amount; | |
default: | |
return state; | |
} | |
} | |
const store = createStore(myReducer); | |
function Component(props) { | |
return ( | |
<div> | |
<div>Store state is currently at {props.storeState}</div> | |
<div> | |
<button onClick={() => props.decreaseBy(5)}>Decrease by five</button> | |
<button onClick={() => props.decreaseBy(1)}>Decrease by one</button> | |
<button onClick={() => props.increaseBy(1)}>Increase by one</button> | |
<button onClick={() => props.increaseBy(5)}>Increase by five</button> | |
</div> | |
</div> | |
); | |
} | |
const ConnectedComponent = connect( | |
state => ({ storeState: state }), | |
{ increaseBy, decreaseBy } | |
)(Component); | |
ReactDOM.render( | |
<Provider store={store}> | |
<ConnectedComponent /> | |
</Provider>, | |
document.getElementById('root') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment