Skip to content

Instantly share code, notes, and snippets.

@brendaMan
Created June 25, 2019 11:21
Show Gist options
  • Save brendaMan/266328c529a1f9e4f92ac8851f495058 to your computer and use it in GitHub Desktop.
Save brendaMan/266328c529a1f9e4f92ac8851f495058 to your computer and use it in GitHub Desktop.
Redux 1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Counter Redux</title>
<link rel="stylesheet" href="style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.1/redux.min.js"></script>
</head>
<body>
<p id="render-store"></p>
<button id="add">+</button>
<button id="remove">-</button>
<button id="addten">+10</button>
<button id="remten">-10</button>
<button id="reset">reset</button>
<script src="main.js"></script>
</body>
</html>
// ACTIONS
const addAction = {
type: 'ADD',
};
const removeAction = {
type: 'REMOVE',
};
const addTen = {
type: 'ADDTEN',
};
const removeTen = {
type: 'REMOVETEN',
};
const resetAction = {
type: 'RESET',
}
// REDUCERS
const counterReducer = (state = 0, action) => {
switch (action.type) {
case 'ADD':
return state + 1;
case 'REMOVE':
return state - 1;
case 'ADDTEN':
return state + 10;
case 'REMOVETEN':
return state - 10;
case 'RESET':
return 0;
default:
return state;
}
}
// STORE
const { createStore} = Redux;
const store = createStore(counterReducer);
// MAIN
const renderStore = document.getElementById('render-store');
const render = () => {
renderStore.innerHTML = store.getState();
}
store.subscribe(render);
render();
const add = document.getElementById('add');
add.addEventListener('click', () => {
store.dispatch(addAction)
});
const remove = document.getElementById('remove');
remove.addEventListener('click', () => {
store.dispatch(removeAction)
});
const addten = document.getElementById('addten');
addten.addEventListener('click', () => {
store.dispatch(addTen)
});
const remten = document.getElementById('remten');
remten.addEventListener('click', () => {
store.dispatch(removeTen)
});
const reset = document.getElementById('reset');
reset.addEventListener('click', () => {
store.dispatch(resetAction)
});
body {
display: flex;
justify-content: center;
}
p, button {
font-size: 3em;
margin: 0.2em;
padding: 0.25em 0.5em;
}
button {
background: #eee;
border: 1px solid rgb(46, 83, 74);
border-radius: 5px;
-webkit-box-shadow: 10px 10px 43px 10px rgba(31,145,130,1);
-moz-box-shadow: 10px 10px 43px 10px rgba(31,145,130,1);
box-shadow: 10px -5px 43px 10px rgba(31,145,130,1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment