Created
February 13, 2020 17:33
-
-
Save r3dm1ke/3384a67a6e08c3a51687d944af42752e to your computer and use it in GitHub Desktop.
Simple redux example
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
| const {createStore} from 'redux'; | |
| // this is how state looks initially | |
| const INITIAL_STATE = { | |
| counter: 0 | |
| }; | |
| function reducer(state=INITIAL_STATE, action) { | |
| // this function will return a new state | |
| // based on action | |
| switch (action.type) { | |
| case 'INCREMENT': | |
| return {...state, counter: state.counter + 1}; | |
| case 'DECREMENT': | |
| return {...state, counter: state.counter - 1}; | |
| default: | |
| return state; | |
| } | |
| } | |
| // createStore function is exported from redux and is used | |
| // to create a redux store | |
| const store = createStore(reducer); | |
| store.dispatch({type: 'INCREMENT'}); // counter = 1 now | |
| store.dispatch({type: 'INCREMENT'}); // counter = 2 now | |
| store.dispatch({type: 'DECREMENT'}); // counter = 1 now |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment