Created
February 13, 2018 17:47
-
-
Save cassiocardoso/b4a08e102bb9ad54f651ec49e49aa9e3 to your computer and use it in GitHub Desktop.
Redux 101
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
import { createStore } from 'redux' | |
/** | |
* This is a reducer, a pure function with (state, action) => state signature. | |
* It describes how an action transforms the state into the next state. | |
* | |
*/ | |
function counter(state = 0, action) { | |
switch (action.type) { | |
case 'INCREMENT': | |
return state + 1 | |
case 'DECREMENT': | |
return state - 1 | |
default: | |
return state | |
} | |
} | |
// Create the store | |
let store = createStore(counter); | |
/** | |
* Actions | |
* | |
*/ | |
store.dispatch({ type: 'INCREMENT' }) | |
// 1 | |
store.dispatch({ type: 'INCREMENT' }) | |
// 2 | |
store.dispatch({ type: 'DECREMENT' }) | |
// 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment