Last active
December 17, 2021 11:08
-
-
Save pratikdevdas/5b3e08736fd0d941be0dcbb2d324c18e to your computer and use it in GitHub Desktop.
Using Redux Increment/Decrement with switch case
This file contains hidden or 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
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
// import App from './App'; | |
import { createStore } from 'redux'; | |
const counterReducer = (state = 0,action) => { | |
switch(action.type){ | |
case 'INCREMENT': | |
return state + 1 | |
case 'DECREMENT': | |
return state - 1 | |
case 'ZERO': | |
return 0 | |
default: | |
return state | |
} | |
} | |
const store = createStore(counterReducer) | |
console.log(store.getState()) | |
store.dispatch({type:'INCREMENT'}) | |
store.dispatch({type:'INCREMENT'}) | |
store.dispatch({type:'INCREMENT'}) | |
console.log(store.getState()) | |
store.dispatch({type:'ZERO'}) | |
store.dispatch({type:'DECREMENT'}) | |
console.log(store.getState()) | |
const App =()=>{ | |
return <>hi</> | |
} | |
ReactDOM.render( | |
<App />, | |
document.getElementById('root') | |
); | |
/* | |
log outputs: | |
0 | |
3 | |
-1*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment