Created
June 15, 2020 19:55
-
-
Save I-keep-trying/5eca709b86b97dd5b6311ba3f2eb6e07 to your computer and use it in GitHub Desktop.
redux counter app
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
import React from 'react' | |
import ReactDOM from 'react-dom' | |
import { createStore } from 'redux' | |
import { Provider } from 'react-redux' | |
import { useDispatch } from 'react-redux' | |
import './index.css' | |
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 = () => { | |
const dispatch = useDispatch() | |
return ( | |
<div> | |
<div>{store.getState()}</div> | |
<button onClick={dispatch(counterReducer({ type: 'INCREMENT' }))}> | |
plus | |
</button> | |
<button onClick={dispatch(counterReducer({ type: 'DECREMENT' }))}> | |
minus | |
</button> | |
<button onClick={dispatch(counterReducer({ type: 'ZERO' }))}>zero</button> | |
</div> | |
) | |
} | |
// RENDER | |
ReactDOM.render( | |
<Provider store={store}> | |
<App /> | |
</Provider>, | |
document.getElementById('root') | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment