Created
July 4, 2017 15:03
-
-
Save crobo-sameer-shemna/896ba7475aef3cea80194c50977f2b96 to your computer and use it in GitHub Desktop.
Redux example Redux Intro // source https://jsbin.com/rifeqiyige
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="Redux Intro"> | |
<meta charset="utf-8"> | |
<title>Redux example</title> | |
<script src="https://npmcdn.com/redux/dist/redux.js"></script> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
const counter = (state = 0, action) => { | |
switch (action.type) { | |
case 'INCREMENT': | |
return state + 1; | |
case 'DECREMENT': | |
return state - 1; | |
case 'RESET': | |
return 0; | |
default: | |
return state; | |
} | |
} | |
const { createStore } = Redux; | |
const store = createStore(counter); | |
const render = () => { | |
document.body.innerHTML = | |
'<h1>Clicked: ' + | |
store.getState() + | |
' times.</h1>' + | |
'<button id="btn_i">Increment</button>' + | |
'<button id="btn_d">Decrement</button>' + | |
'<button id="btn_r">Reset</button>' | |
; | |
document.getElementById('btn_i').addEventListener('click', () => { | |
store.dispatch({ type: 'INCREMENT' }); | |
}); | |
document.getElementById('btn_d').addEventListener('click', () => { | |
store.dispatch({ type: 'DECREMENT' }); | |
}); | |
document.getElementById('btn_r').addEventListener('click', () => { | |
store.dispatch({ type: 'RESET' }); | |
}); | |
}; | |
store.subscribe(render); | |
render(); | |
</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; | |
case 'RESET': | |
return 0; | |
default: | |
return state; | |
} | |
} | |
const { createStore } = Redux; | |
const store = createStore(counter); | |
const render = () => { | |
document.body.innerHTML = | |
'<h1>Clicked: ' + | |
store.getState() + | |
' times.</h1>' + | |
'<button id="btn_i">Increment</button>' + | |
'<button id="btn_d">Decrement</button>' + | |
'<button id="btn_r">Reset</button>' | |
; | |
document.getElementById('btn_i').addEventListener('click', () => { | |
store.dispatch({ type: 'INCREMENT' }); | |
}); | |
document.getElementById('btn_d').addEventListener('click', () => { | |
store.dispatch({ type: 'DECREMENT' }); | |
}); | |
document.getElementById('btn_r').addEventListener('click', () => { | |
store.dispatch({ type: 'RESET' }); | |
}); | |
}; | |
store.subscribe(render); | |
render(); | |
</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const counter = (state = 0, action) => { | |
switch (action.type) { | |
case 'INCREMENT': | |
return state + 1; | |
case 'DECREMENT': | |
return state - 1; | |
case 'RESET': | |
return 0; | |
default: | |
return state; | |
} | |
} | |
const { createStore } = Redux; | |
const store = createStore(counter); | |
const render = () => { | |
document.body.innerHTML = | |
'<h1>Clicked: ' + | |
store.getState() + | |
' times.</h1>' + | |
'<button id="btn_i">Increment</button>' + | |
'<button id="btn_d">Decrement</button>' + | |
'<button id="btn_r">Reset</button>' | |
; | |
document.getElementById('btn_i').addEventListener('click', () => { | |
store.dispatch({ type: 'INCREMENT' }); | |
}); | |
document.getElementById('btn_d').addEventListener('click', () => { | |
store.dispatch({ type: 'DECREMENT' }); | |
}); | |
document.getElementById('btn_r').addEventListener('click', () => { | |
store.dispatch({ type: 'RESET' }); | |
}); | |
}; | |
store.subscribe(render); | |
render(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment