Skip to content

Instantly share code, notes, and snippets.

@LuisRBarreras
Created July 25, 2019 14:16
Show Gist options
  • Save LuisRBarreras/9febc58f83df6c26b8da0ca1bde9352d to your computer and use it in GitHub Desktop.
Save LuisRBarreras/9febc58f83df6c26b8da0ca1bde9352d to your computer and use it in GitHub Desktop.
JS Bin Redux Course // source https://jsbin.com/someqil
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Redux Course">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bonsai/0.4/bonsai.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect/1.20.2/expect.min.js"></script>
<script src="https://fb.me/react-15.1.0.js"></script>
<script src="https://fb.me/react-dom-15.1.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.4/redux.min.js"></script>
<div id="root"></div>
<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;
}
};
// const creatStore = (reducer) => {
// let state;
// let listeners = [];
// 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);
// }
// }
// return { getState, dispatch, subscribe };
// }
var Counter = function Counter(_ref) {
var value = _ref.value;
var onIncrement = _ref.onIncrement;
var onDecrement = _ref.onDecrement;
return React.createElement(
'div',
null,
React.createElement(
'h1',
null,
value
),
React.createElement(
'button',
{ onClick: onIncrement },
'+'
),
React.createElement(
'button',
{ onClick: onDecrement },
'-'
)
);
};
var _Redux = Redux;
var createStore = _Redux.createStore;
var store = createStore(counter);
var render = function render() {
ReactDOM.render(React.createElement(Counter, { value: store.getState(),
onIncrement: function () {
store.dispatch({ 'type': 'INCREMENT' });
},
onDecrement: function () {
store.dispatch({ 'type': 'DECREMENT' });
}
}), document.getElementById('root'));
};
store.subscribe(render);
render();
console.log('Test passed!');
</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 creatStore = (reducer) => {
// let state;
// let listeners = [];
// 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);
// }
// }
// return { getState, dispatch, subscribe };
// }
const Counter = ({ value, onIncrement, onDecrement }) => (
<div>
<h1>{value}</h1>
<button onClick={onIncrement}>+</button>
<button onClick={onDecrement}>-</button>
</div>
)
const { createStore } = Redux;
const store = createStore(counter);
const render = () => {
ReactDOM.render(
<Counter value={store.getState()}
onIncrement={() => {
store.dispatch({ 'type': 'INCREMENT' })
}}
onDecrement={() => {
store.dispatch({ 'type': 'DECREMENT' })
}}
/>,
document.getElementById('root')
);
}
store.subscribe(render);
render();
console.log('Test passed!');</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;
}
};
// const creatStore = (reducer) => {
// let state;
// let listeners = [];
// 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);
// }
// }
// return { getState, dispatch, subscribe };
// }
var Counter = function Counter(_ref) {
var value = _ref.value;
var onIncrement = _ref.onIncrement;
var onDecrement = _ref.onDecrement;
return React.createElement(
'div',
null,
React.createElement(
'h1',
null,
value
),
React.createElement(
'button',
{ onClick: onIncrement },
'+'
),
React.createElement(
'button',
{ onClick: onDecrement },
'-'
)
);
};
var _Redux = Redux;
var createStore = _Redux.createStore;
var store = createStore(counter);
var render = function render() {
ReactDOM.render(React.createElement(Counter, { value: store.getState(),
onIncrement: function () {
store.dispatch({ 'type': 'INCREMENT' });
},
onDecrement: function () {
store.dispatch({ 'type': 'DECREMENT' });
}
}), document.getElementById('root'));
};
store.subscribe(render);
render();
console.log('Test passed!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment