Created
October 25, 2020 00:27
-
-
Save Aldhanekaa/9beed9186ed5248754a5fa34d6303ffe to your computer and use it in GitHub Desktop.
First Redux app
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 INCREMENT = 'INCREMENT', // Define a constant for increment action types | |
| DECREMENT = 'DECREMENT', // Define a constant for decrement action types | |
| MULTIPLY = 'MULTIPLY', | |
| DIVIDE = 'DIVIDE'; | |
| const counterReducer = (state= 0, action) => { | |
| switch(action.type) { | |
| case INCREMENT : | |
| return state + 1 | |
| case DECREMENT : | |
| return state - 1 | |
| case MULTIPLY : | |
| return state * action.num | |
| case DIVIDE : | |
| return state / action.num | |
| } | |
| }; // Define the counter reducer which will increment or decrement the state based on the action it receives | |
| const incAction = () => ({type:INCREMENT}), // Define an action creator for incrementing | |
| decAction = () => ({type: DECREMENT}), // Define an action creator for decrementing | |
| multiplyAction = (n) => ({type: MULTIPLY, num : n}), | |
| divideAction = (n) => ({type: DIVIDE, num:n}); | |
| const store = Redux.createStore(counterReducer); // Define the Redux store here, passing in your reducers | |
| store.subscribe(() => console.log(store.getState())) | |
| store.dispatch(decAction()) | |
| store.dispatch(incAction()) | |
| store.dispatch(incAction()) | |
| store.dispatch(incAction()) | |
| store.dispatch(multiplyAction(2)) | |
| store.dispatch(divideAction(2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment