Skip to content

Instantly share code, notes, and snippets.

@seungdols
Created September 7, 2018 06:21
Show Gist options
  • Save seungdols/e8462a37eb307e65175d7649d0c2869a to your computer and use it in GitHub Desktop.
Save seungdols/e8462a37eb307e65175d7649d0c2869a to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/vitaruk
<!DOCTYPE html>
<html>
<head>
<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/redux/4.0.0/redux.js"></script>
<script id="jsbin-javascript">
//console.log(Redux);
const MY_INCREMENT = 'INCREMENT';
const MY_DECREMENT = 'DECREMENT';
const increment = (data) => ({
type: MY_INCREMENT,
diff: data
});
const decrement = (data) => ({
type: MY_DECREMENT,
diff: data
});
console.log(increment(1));
const initialState = {
number: 0
}
function counter(state = initialState, action) {
switch(action.type) {
case MY_INCREMENT:
return { number: state.number + action.diff }
case MY_DECREMENT:
return { number: state.number - action.diff }
default:
return state;
}
}
const initialStateWithObj = {
number: 0,
foo: 'bar',
baz: 'qux'
}
function counter2(state = initialStateWithObj, action) {
switch(action.type) {
case MY_INCREMENT:
return Object.assign({}, state, { number: state.number + action.diff })
case MY_DECREMENT:
return Object.assign({}, state, { number: state.number - action.diff })
default:
return state;
}
}
//console.log(counter(undefined, increment(2)));
const { createStore } = Redux;
const store = createStore(counter);
const unsubscribe = store.subscribe(() => {
console.log(store.getState())
});
store.dispatch(increment(1));
store.dispatch(decrement(5));
store.dispatch(increment(10));
</script>
<script id="jsbin-source-javascript" type="text/javascript">//console.log(Redux);
const MY_INCREMENT = 'INCREMENT';
const MY_DECREMENT = 'DECREMENT';
const increment = (data) => ({
type: MY_INCREMENT,
diff: data
});
const decrement = (data) => ({
type: MY_DECREMENT,
diff: data
});
console.log(increment(1));
const initialState = {
number: 0
}
function counter(state = initialState, action) {
switch(action.type) {
case MY_INCREMENT:
return { number: state.number + action.diff }
case MY_DECREMENT:
return { number: state.number - action.diff }
default:
return state;
}
}
const initialStateWithObj = {
number: 0,
foo: 'bar',
baz: 'qux'
}
function counter2(state = initialStateWithObj, action) {
switch(action.type) {
case MY_INCREMENT:
return Object.assign({}, state, { number: state.number + action.diff })
case MY_DECREMENT:
return Object.assign({}, state, { number: state.number - action.diff })
default:
return state;
}
}
//console.log(counter(undefined, increment(2)));
const { createStore } = Redux;
const store = createStore(counter);
const unsubscribe = store.subscribe(() => {
console.log(store.getState())
});
store.dispatch(increment(1));
store.dispatch(decrement(5));
store.dispatch(increment(10));
</script></body>
</html>
//console.log(Redux);
const MY_INCREMENT = 'INCREMENT';
const MY_DECREMENT = 'DECREMENT';
const increment = (data) => ({
type: MY_INCREMENT,
diff: data
});
const decrement = (data) => ({
type: MY_DECREMENT,
diff: data
});
console.log(increment(1));
const initialState = {
number: 0
}
function counter(state = initialState, action) {
switch(action.type) {
case MY_INCREMENT:
return { number: state.number + action.diff }
case MY_DECREMENT:
return { number: state.number - action.diff }
default:
return state;
}
}
const initialStateWithObj = {
number: 0,
foo: 'bar',
baz: 'qux'
}
function counter2(state = initialStateWithObj, action) {
switch(action.type) {
case MY_INCREMENT:
return Object.assign({}, state, { number: state.number + action.diff })
case MY_DECREMENT:
return Object.assign({}, state, { number: state.number - action.diff })
default:
return state;
}
}
//console.log(counter(undefined, increment(2)));
const { createStore } = Redux;
const store = createStore(counter);
const unsubscribe = store.subscribe(() => {
console.log(store.getState())
});
store.dispatch(increment(1));
store.dispatch(decrement(5));
store.dispatch(increment(10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment