Skip to content

Instantly share code, notes, and snippets.

@I-keep-trying
Created June 15, 2020 19:55
Show Gist options
  • Save I-keep-trying/5eca709b86b97dd5b6311ba3f2eb6e07 to your computer and use it in GitHub Desktop.
Save I-keep-trying/5eca709b86b97dd5b6311ba3f2eb6e07 to your computer and use it in GitHub Desktop.
redux counter app
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