Last active
November 6, 2015 00:50
-
-
Save fxck/3db6ac0632f515536570 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 { 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. | |
* | |
* The shape of the state is up to you: it can be a primitive, an array, an object, | |
* or even an Immutable.js data structure. The only important part is that you should | |
* not mutate the state object, but return a new object if the state changes. | |
* | |
* In this example, we use a `switch` statement and strings, but you can use a helper that | |
* follows a different convention (such as function maps) if it makes sense for your project. | |
*/ | |
function counter(state = 0, action) { | |
switch (action.type) { | |
case 'INCREMENT': | |
return state + 1 | |
case 'DECREMENT': | |
return state - 1 | |
default: | |
return state | |
} | |
} | |
// Create a Redux store holding the state of your app. | |
// Its API is { subscribe, dispatch, getState }. | |
let store = createStore(counter) | |
// You can subscribe to the updates manually, or use bindings to your view layer. | |
store.subscribe(() => | |
console.log(store.getState()) | |
) | |
// The only way to mutate the internal state is to dispatch an action. | |
// The actions can be serialized, logged or stored and later replayed. | |
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