Created
December 9, 2021 08:46
-
-
Save pratikdevdas/4f46e37ab5b624e513c1248175080704 to your computer and use it in GitHub Desktop.
Simple counter app with redux
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 { 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) | |
const App = () => { | |
return ( | |
<div> | |
<div> | |
{store.getState()} | |
</div> | |
<button | |
onClick={e => store.dispatch({type:'INCREMENT'})} | |
> | |
plus | |
</button> | |
<button | |
onClick={e => store.dispatch({type:'DECREMENT'})} | |
> | |
minus | |
</button> | |
<button | |
onClick={e => store.dispatch({type:'ZERO'})} | |
> | |
zero | |
</button> | |
</div> | |
); | |
} | |
const renderApp = () => { | |
ReactDOM.render( | |
<App />, | |
document.getElementById('root') | |
)} | |
renderApp() | |
store.subscribe(renderApp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment