Last active
November 23, 2021 07:09
-
-
Save gokhancerk/9b6b55872fd6d8230213e74afd40e92a to your computer and use it in GitHub Desktop.
react-redux cheetsheet
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
| // reducer | |
| const person = { | |
| name: '', | |
| age: '', | |
| national:'' | |
| } | |
| export const personReducer = (state = person, action) => { | |
| switch (action.type) { | |
| case 'NAME': | |
| return {...state, name: action.payload} | |
| default: | |
| return state; | |
| } | |
| } | |
| // store/index.js | |
| import {createStore} from 'redux | |
| export const store = createStore(reducer) | |
| // src/index.js | |
| import {Provider} from 'react-redux' | |
| import {store} from 'store/' | |
| <Provider store={store}> | |
| <App /> | |
| </Provider> | |
| // App.js | |
| import {useDispatch, useSelector} from 'react-redux' | |
| const name = useSelector(state => state.name); | |
| const dispatch = useDispatch() | |
| dispatch({type:'name', payload: 'Alex'}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment