Created
March 6, 2020 18:16
-
-
Save r3dm1ke/8436d692a07b17339e3d4b0005aa67f8 to your computer and use it in GitHub Desktop.
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
| // EXAMPLE WITH REDUX | |
| import React from 'react'; | |
| import {createStore} from 'redux'; | |
| import {Provider, useSelector, useDispatch} from 'react-redux'; | |
| const initialState = {counter: 0}; | |
| const reducer = (state=initialState, action) => { | |
| switch (action.type) { | |
| case 'INCREMENT': | |
| return {...state, counter: state.counter + 1}; | |
| default: | |
| return state; | |
| } | |
| }; | |
| const store = createStore(reducer); | |
| const App = () => { | |
| return ( | |
| <Provider store={store}> | |
| <Counter /> | |
| </Provider> | |
| ); | |
| } | |
| const Counter = () => { | |
| const counter = useSelector(state => state.counter); | |
| const dispatch = useDispatch(); | |
| return ( | |
| <> | |
| <button onClick={() => dispatch({type: 'INCREMENT'})}>Click me</button> | |
| <p>You clicked me {counter} times</p> | |
| </> | |
| ); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment