A Pen by Etienne Deladonchamps on CodePen.
Last active
September 10, 2017 09:40
-
-
Save etienne-dldc/7923f11c51d604066d9ea01b6dfb1cc2 to your computer and use it in GitHub Desktop.
18 - Redux with React
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
<div id="app"></div> |
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
// reducer | |
function counterReducer(state = 0, action) { | |
switch (action.type) { | |
case 'INCREMENT': | |
return state + 1 | |
case 'DECREMENT': | |
return state - 1 | |
default: | |
return state | |
} | |
} | |
// Counter component | |
const Counter = props => | |
<div> | |
<span>{props.value}</span> | |
{props.value > 0 && | |
<button | |
onClick={() => { | |
props.onDecrementClicked(); | |
}} | |
> | |
- | |
</button>} | |
{(props.value < 10) && | |
<button | |
onClick={() => { | |
props.onIncrementClicked(); | |
}} | |
> | |
+ | |
</button>} | |
</div>; | |
// create the store | |
let store = Redux.createStore(counterReducer) | |
function render() { | |
ReactDOM.render( | |
<Counter | |
value={store.getState()} | |
onIncrementClicked={() => store.dispatch({ type: 'INCREMENT' })} | |
onDecrementClicked={() => store.dispatch({ type: 'DECREMENT' })} | |
/>, | |
document.getElementById('app') | |
) | |
} | |
// subscribe | |
store.subscribe(() => { | |
render(); | |
}) | |
// initial render | |
render() |
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
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.7.2/redux.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.min.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment