Lesson 9 - Reactjs (VTI)
Lesson 9 - Reactjs (VTI)
React JS
VTI ACADEMY
Author: duy.nguyenngoc1@vti.com.vn
March 2020
Agenda
❑ Redux Core
❑ React Redux
❑ Assignments
Container that manages the Global State of the entire application through the Store (similar to Context)
o State is Read-only
Flow
https://redux.js.org/assets/images/ReduxDataFlowDiagram-49fa8
c3968371d9ef6f2a1486bd40a26.gif
Example 1:
o Normal
Example 1:
o Redux
Self-practice
Example 2: Structuring
Structure
Usually create 1 Root Reducer containing Child Reducer
Each Child Reducer is responsible for changing an attribute in the state
Accessing state: state.todo.todos
Define action.type as constant
Create ActionCreator objects to return actions required for dispatch Creates Selector objects to return values which getted from store
Components
Self-practice
Components
o Store
o Action
o Reducer
o Action Creators
o Selectors
https://redux.js.org/assets/images/ReduxDataFlowDiagram-49fa8
c3968371d9ef6f2a1486bd40a26.gif
Store (object)
o Available method
▪ store.getState()
▪ store.dispatch(action)
▪ store.subscribe(callback function)
Action (object)
o Include:
▪ type (required)
▪ payload (optional)
Reducer (function)
o The place to receive action and change the state in the store if needed
o Do not edit state directly 🡺 copy out a separate clone and edit
Reducer (function)
o 2 types
▪ Root Reducer
▪ Child Reducer
Selectors (function)
o createStore()
o combineReducers()
o store.getState()
o store.dispatch()
o store.subscribe()
o Redux Store should only store Global State, React Component will store Local State
mapStateToProps() mapDispatchToProps()
Self-practice
o Provider
o connect()
▪ mapStateToProps
▪ mapDispatchToProps
connect()
o Include:
▪ mapStateToProps (optional)
mapStateToProps (optional)
o Mapping Global State into Component's props
o Called every time the Global State in the Store changes
o Automatically subscribe and re-render when the Global State in the store changes
state(required) và ownProps (optional)
o 2 ways:
▪ props.dispatch
▪ mapDispatchToProps() (recommended)
ownprops
https://react-redux.js.org/using-react-redux/accessing-store#providing-custom-context
Multiple Stores
https://react-redux.js.org/using-react-redux/accessing-store#multiple-stores
Selector
Action creator
Self-practice
Hooks
o useSelector()
o useDispatch()
o useStore()
Redux Core
React Hook
⮚https://reactjs.org/docs/getting-started.html
⮚https://www.javatpoint.com/reactjs-tutorial
⮚https://www.udemy.com/course/react-the-complete-guide-incl-redux