Created
April 5, 2016 17:40
-
-
Save rameshanandakrishnan/5d2bbaf34627c1a99dcad0dd1b628e23 to your computer and use it in GitHub Desktop.
First react counter example without 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
function counter(state, action) { | |
if (typeof state === 'undefined') { | |
return 0; | |
} | |
if (action.type === 'INCREMENT') { | |
return state + 1; | |
} else if (action.type === 'DECREMENT'){ | |
return state -1; | |
}else { | |
return state; | |
} | |
} | |
var createStore = Redux.createStore; | |
var store = createStore(counter); | |
console.log(store.getState()); | |
store.dispatch({ type: 'INCREMENT' }); | |
console.log(store.getState()); | |
var render = function(){ | |
document.body.innerText = store.getState(); | |
}; | |
store.subscribe(render); | |
render(); | |
document.addEventListener('click', function(){ | |
store.dispatch({type: 'INCREMENT'}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment