Created
February 29, 2016 23:38
-
-
Save anonymous/d22c4f4c882f44acf330 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/tumufo
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.0.4/redux.js"></script> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
'use strict'; | |
var counter = function counter(state, action) { | |
if (state === undefined) state = 0; | |
switch (action.type) { | |
case 'INCREMENT': | |
return state + 1; | |
case 'DECREMENT': | |
return state - 1; | |
default: | |
return state; | |
} | |
}; | |
var createStore = function createStore(reducer) { | |
var listeners = []; | |
var state = undefined; | |
var getState = function getState() { | |
return state; | |
}; | |
var dispatch = function dispatch(action) { | |
state = reducer(state, action); | |
listeners.forEach(function (listener) { | |
return listener(); | |
}); | |
}; | |
var subscribe = function subscribe(listener) { | |
listeners.push(listener); | |
return function () { | |
listeners = listeners.filter(function (l) { | |
return l !== listener; | |
}); | |
}; | |
}; | |
// Call reducer function to get initial value | |
dispatch({}); | |
return { getState: getState, dispatch: dispatch, subscribe: subscribe }; | |
}; | |
// const { createStore } = Redux; | |
// var createStore = Redux.createStore; | |
// import { createStore } from 'redux'; | |
// Store needs reducer function - it transforms state | |
var store = createStore(counter); | |
var render = function render() { | |
document.body.innerText = store.getState(); | |
}; | |
// 3 IMPORTANT STORE METHODS | |
// 1. GetState - gets current state of redux store | |
console.log(store.getState()); | |
// 2. Dispatch - dispatches action (to change state of app data) | |
store.dispatch({ type: 'INCREMENT' }); | |
console.log(store.getState()); | |
// 3. Subscribe - registers a functions to be called everytime store receives ACTION | |
store.subscribe(function () { | |
render(); | |
}); | |
render(); // Render initial state, it will be 1, becouse already dispatched one action | |
document.addEventListener('click', function () { | |
store.dispatch({ type: 'INCREMENT' }); | |
}); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const counter = (state = 0, action) => { | |
switch (action.type) { | |
case 'INCREMENT': | |
return state + 1; | |
case 'DECREMENT': | |
return state - 1; | |
default: | |
return state; | |
} | |
} | |
const createStore = (reducer) => { | |
let listeners = []; | |
let state; | |
const getState = () => state; | |
const dispatch = (action) => { | |
state = reducer(state, action); | |
listeners.forEach(listener => listener()); | |
}; | |
const subscribe = (listener) => { | |
listeners.push(listener); | |
return () => { | |
listeners = listeners.filter(l => l !== listener) | |
}; | |
}; | |
// Call reducer function to get initial value | |
dispatch({}); | |
return { getState, dispatch, subscribe }; | |
} | |
// const { createStore } = Redux; | |
// var createStore = Redux.createStore; | |
// import { createStore } from 'redux'; | |
// Store needs reducer function - it transforms state | |
const store = createStore(counter); | |
const render = () => { | |
document.body.innerText = store.getState(); | |
}; | |
// 3 IMPORTANT STORE METHODS | |
// 1. GetState - gets current state of redux store | |
console.log(store.getState()); | |
// 2. Dispatch - dispatches action (to change state of app data) | |
store.dispatch({type: 'INCREMENT' }); | |
console.log(store.getState()); | |
// 3. Subscribe - registers a functions to be called everytime store receives ACTION | |
store.subscribe(() => { | |
render(); | |
}); | |
render(); // Render initial state, it will be 1, becouse already dispatched one action | |
document.addEventListener('click', () => { | |
store.dispatch({ type: 'INCREMENT'}) | |
});</script></body> | |
</html> |
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
'use strict'; | |
var counter = function counter(state, action) { | |
if (state === undefined) state = 0; | |
switch (action.type) { | |
case 'INCREMENT': | |
return state + 1; | |
case 'DECREMENT': | |
return state - 1; | |
default: | |
return state; | |
} | |
}; | |
var createStore = function createStore(reducer) { | |
var listeners = []; | |
var state = undefined; | |
var getState = function getState() { | |
return state; | |
}; | |
var dispatch = function dispatch(action) { | |
state = reducer(state, action); | |
listeners.forEach(function (listener) { | |
return listener(); | |
}); | |
}; | |
var subscribe = function subscribe(listener) { | |
listeners.push(listener); | |
return function () { | |
listeners = listeners.filter(function (l) { | |
return l !== listener; | |
}); | |
}; | |
}; | |
// Call reducer function to get initial value | |
dispatch({}); | |
return { getState: getState, dispatch: dispatch, subscribe: subscribe }; | |
}; | |
// const { createStore } = Redux; | |
// var createStore = Redux.createStore; | |
// import { createStore } from 'redux'; | |
// Store needs reducer function - it transforms state | |
var store = createStore(counter); | |
var render = function render() { | |
document.body.innerText = store.getState(); | |
}; | |
// 3 IMPORTANT STORE METHODS | |
// 1. GetState - gets current state of redux store | |
console.log(store.getState()); | |
// 2. Dispatch - dispatches action (to change state of app data) | |
store.dispatch({ type: 'INCREMENT' }); | |
console.log(store.getState()); | |
// 3. Subscribe - registers a functions to be called everytime store receives ACTION | |
store.subscribe(function () { | |
render(); | |
}); | |
render(); // Render initial state, it will be 1, becouse already dispatched one action | |
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