Created
November 26, 2015 01:10
-
-
Save kenichi-shibata/878a9fde5ba7902bc496 to your computer and use it in GitHub Desktop.
redux basics from https://jsbin.com/fizibo/edit?js,console
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
const counter = (state = 0, action) => { | |
switch(action.type){ | |
case 'INCREMENT': | |
return state + 1; | |
case 'DECREMENT': | |
return state - 1; | |
default: | |
return state; | |
} | |
} | |
//created a basic counter using redux | |
const { createStore } = Redux; | |
// var createStore = Redux.createStore; //es5 | |
// import { createStore } from 'redux' //babel import | |
const store = createStore(counter); | |
console.log(store.getState()); | |
// created a new reducer using pure function counter | |
store.dispatch({type: 'INCREMENT' }); | |
console.log(store.getState()); | |
store.dispatch({type: 'DECREMENT'}); | |
console.log(store.getState()); | |
store.dispatch({type: 'DECREMENT'}); | |
console.log(store.getState()); | |
store.dispatch({type: 'INCREMENT' }); | |
console.log(store.getState()); | |
store.dispatch({type: 'INCREMENT' }); | |
console.log(store.getState()); | |
store.dispatch({type: 'INCREMENT' }); | |
console.log(store.getState()); | |
// redux does not set State it returns the state to its original state after execution (immutable) | |
// of course you need to add redux dependencies |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment