React Redux State Management
Veröffentlicht am: 07. April 2020
Lesedauer: 1 Min.
import {createStore} from 'redux' import {useSelector, useDispatch} from 'redux'
Store & initialStore Actions & ActionCreators Reducer / RootReducer Provider / wrap entire Application
- in initial_store.js create initialStore / initialState const initialStore = {}
- in actions.js create the actions export const EXAMPLEACTION = "EXAMPLEACTION" export const OTHERACTION = "OTHERACTION" export const SOMEACTION = "SOMEACTION"
- in reducer.js create the reducer
import {EXAMPLE_ACTION} from './action.js'
const reducer = (state, action) => { if(action.type === EXAMPLE_ACTION) { return {} } return { state } }
- const store = createStore(reducer = initialStore)
Store takes 2 Arguments
dispatch() method sends the actions to the Store
Reducer does NOT mutate the state, it returns a new State Reducer is a Function and takes 2 Arguments function reducer(oldState, action) { console.log({oldState, action}) } Reducer updates the Store
mapStateToProps mapDispatchToProps
Hooks version
useSelector useDispatch
Articles in related Categories
16. April 2020
13. April 2020
Google Tag Manager
Axios
Redux
Express
Node.js
Git
HTML
GraphQL
Javascript
styled-components
Material UI
CSS
Gatsby
React