Skip to content

Instantly share code, notes, and snippets.

Created February 29, 2016 23:29
Show Gist options
  • Save anonymous/7c5083863a58db74ecbb to your computer and use it in GitHub Desktop.
Save anonymous/7c5083863a58db74ecbb to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/tumufo
<!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 _Redux = Redux;
var createStore = _Redux.createStore;
// 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 } = 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>
'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 _Redux = Redux;
var createStore = _Redux.createStore;
// 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