Skip to content

Instantly share code, notes, and snippets.

@AlenaNik
Last active June 30, 2019 17:13
Show Gist options
  • Save AlenaNik/5d0532bfe066ad1863c638b47fc935a6 to your computer and use it in GitHub Desktop.
Save AlenaNik/5d0532bfe066ad1863c638b47fc935a6 to your computer and use it in GitHub Desktop.
ReduxQuest
body {
display: flex;
flex-direction: column;
justify-content: center;
}
p, button {
font-size: 2em;
margin: 0.2em;
padding: 0.25em 0.5em;
}
button {
background: #eee;
border: 1px solid #eee;
border-radius: 5px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Counter Redux</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Redux CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.1/redux.min.js"></script>
</head>
<body>
<!-- Render the store -->
<p id="counter"></p>
<button id="add">Add1</button>
<button id="remove">Remove1</button>
<button id="addTen">Add10</button>
<button id="removeTen">Remove10</button>
<button id="reset">Reset</button>
</body>
</html>
function counter(state=0, action) {
switch (action.type) {
case 'ADD':
return state + 1
break;
case 'REMOVE':
return state - 1
break;
case 'ADDTEN':
return state + 10
break;
case 'REMOVETEN':
return state - 10
break;
case 'RESET':
return 0
break;
default:
return state;
}
}
// STORE
let store = Redux.createStore(counter);
// MAIN
function render() {
document.querySelector('#counter').innerText = store.getState();
}
store.subscribe(render);
render();
document.querySelector('#add').addEventListener('click', () => {
store.dispatch({type: 'ADD'});
});
document.querySelector('#remove').addEventListener('click', () => {
store.dispatch({type: 'REMOVE'});
});
document.querySelector('#addTen').addEventListener('click', () => {
store.dispatch({type: 'ADDTEN'});
});
document.querySelector('#removeTen').addEventListener('click', () => {
store.dispatch({type: 'REMOVETEN'});
});
document.querySelector('#reset').addEventListener('click', () => {
store.dispatch({type: 'RESET'});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment