Created
June 6, 2016 12:30
-
-
Save franzejr/5409318a8d2c76b8c352be64cdc8d850 to your computer and use it in GitHub Desktop.
Simple example using Redux and React - dispatching actions
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>JS Bin</title> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.3.1/redux.js"></script> | |
| <script src="https://fb.me/react-0.14.7.js"></script> | |
| <script src="https://fb.me/react-dom-0.14.7.js"></script> | |
| </head> | |
| <body> | |
| <div id='root'></div> | |
| </body> | |
| </html> |
This file contains hidden or 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 counter = (state = 0, action) => { | |
| switch (action.type) { | |
| case 'INCREMENT': | |
| return state + 1; | |
| case 'DECREMENT': | |
| return state - 1; | |
| default: | |
| return state; | |
| } | |
| } | |
| const Counter = ({ | |
| value, | |
| onIncrement, | |
| onDecrement | |
| }) => ( | |
| <div> | |
| <h1>{value}</h1> | |
| <button onClick={onIncrement}>+</button> | |
| <button onClick={onDecrement}>-</button> | |
| </div> | |
| ); | |
| const { createStore } = Redux; | |
| const store = createStore(counter); | |
| const render = () => { | |
| ReactDOM.render( | |
| <Counter | |
| value={store.getState()} | |
| onIncrement={() => | |
| store.dispatch({ | |
| type: 'INCREMENT' | |
| }) | |
| } | |
| onDecrement={() => | |
| store.dispatch({ | |
| type: 'DECREMENT' | |
| }) | |
| } | |
| />, | |
| document.getElementById('root') | |
| ); | |
| }; | |
| store.subscribe(render); | |
| render(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment