自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

手寫React-Redux核心原理,再也不怕面試官問我React-Redux原理

運維 數(shù)據(jù)庫運維
當(dāng)一個react項目組件層級越來越深,頁面越來越多的時候,數(shù)據(jù)在各個組件層級和頁面之間傳遞的需求就會比較多,很多變量也需要做成可全局管理的。在這個時候,redux和react-redux的使用就很有必要了。它們能幫助我們很方便的進行項目全局性的數(shù)據(jù)管理。

[[347940]]

下面,就寫一下我自己對 redux 和 React-redux 的學(xué)習(xí)以及使用的心得,權(quán)當(dāng)是對學(xué)習(xí)過程的一種記錄和分享。

一、redux和React-redux的幾個重要概念
1.1 action
Action 是把數(shù)據(jù)從應(yīng)用(這里之所以不叫 view 是因為這些數(shù)據(jù)有可能是服務(wù)器響應(yīng),用戶輸入或其它非 view 的數(shù)據(jù) )傳到 store 的有效載荷。它是 store 數(shù)據(jù)的唯一來源。一般來說你會通過 store.dispatch() 將 action 傳到 store。

1.2 reducer
Reducers 指定了應(yīng)用狀態(tài)的變化如何響應(yīng) actions并發(fā)送到 store 的,記住 actions 只是描述了有事情發(fā)生了這一事實,并沒有描述應(yīng)用如何更新 state。

1.3 store
store就是把action和reducer聯(lián)系到一起的對象,store本質(zhì)上是一個狀態(tài)樹,保存了所有對象的狀態(tài)。任何UI組件都可以直接從store訪問特定對象的狀態(tài)。

在 Redux 中,所有的數(shù)據(jù)(比如state)被保存在一個store容器中 ,在一個應(yīng)用程序中只能有一個store對象。當(dāng)一個store接收到一個action,它將把這個action代理給相關(guān)的reducer。reducer是一個純函數(shù),它可以查看之前的狀態(tài),執(zhí)行一個action并且返回一個新的狀態(tài)。

1.4 Provider
Provider 其實就只是一個外層容器,它的作用就是通過配合 connect 來達到跨層級傳遞數(shù)據(jù)。使用時只需將Provider定義為整個項目最外層的組件,并設(shè)置好store。那么整個項目都可以直接獲取這個store。它的原理其實是通過React中的[Context]()來實現(xiàn)的。它大致的核心代碼如下:

  1. import React, {Component} from 'react' 
  2. import {PropTypes} from 'prop-types' 
  3.  
  4. export default class Provider extends Component { 
  5.     getChildContext() { 
  6.         return {store: this.props.store} 
  7.     } 
  8.  
  9.     constructor() { 
  10.         super() 
  11.  
  12.         this.state = {} 
  13.     } 
  14.  
  15.     render() { 
  16.         return this.props.children 
  17.     } 
  18.  
  19. Provider.childContextTypes = { 
  20.     store: PropTypes.object 

1.5 connect
connect 的作用是連接React組件與 Redux store,它包在我們的容器組件的外一層,它接收上面 Provider 提供的 store 里面的 state 和 dispatch,傳給一個構(gòu)造函數(shù),返回一個對象,以屬性形式傳給我們的容器組件。

它共有四個參數(shù)mapStateToProps, mapDispatchToProps, mergeProps以及options。

mapStateToProps 的作用是將store里的state(數(shù)據(jù)源)綁定到指定組件的props中 mapDispatchToProps 的作用是將store里的action(操作數(shù)據(jù)的方法)綁定到指定組件的props中 另外兩個方法一般情況下使用不到,這里就不做介紹。。

那么 connect 是怎么將React組件與 Redux store連接起來的呢?其主要邏輯可以總結(jié)成以下代碼:

  1. import {Component} from "react"
  2. import React from "react"
  3. import {PropTypes} from 'prop-types' 
  4.  
  5. const connect = (mapStateToProps, mapDispatchToProps) => (WrappedComponent => { 
  6.     class Connect extends Component { 
  7.         constructor() { 
  8.             super() 
  9.  
  10.             this.state = {} 
  11.  
  12.         } 
  13.  
  14.         componentWillMount() { 
  15.             this.unSubscribe = this.context.store.subscribe(() => { 
  16.                 this.setState(mapStateToProps(this.context.store.getState())) 
  17.             }) 
  18.         } 
  19.  
  20.         componentWillUnmount() { 
  21.             this.unSubscribe() 
  22.         } 
  23.  
  24.         render() { 
  25.             return <WrappedComponent  {...this.state} 
  26.                                       {...mapDispatchToProps(this.context.store.dispatch)}/> 
  27.         } 
  28.     } 
  29.  
  30.     Connect.contextTypes = { 
  31.         store: PropTypes.object 
  32.     } 
  33.     return Connect 
  34. }) 
  35.  
  36. export default connect 

二、redux和React-redux的使用
項目中關(guān)于redux的文件夾目錄如下

拿管理用戶信息數(shù)據(jù)的需求來舉例

第一步,編寫操作用戶信息的action

  1. import {USER_INFO} from "../constants/actionTypes"
  2. import store from '../store/store' 
  3.  
  4. export const switchUser = (data) => { 
  5.     console.log("switchUser()",data); 
  6.     return () => { 
  7.         store.dispatch({ 
  8.             type: USER_INFO, 
  9.             ...data 
  10.         }) 
  11.     } 

第二步,編寫改變用戶信息并返回新state的reducer

  1. import {USER_INFO} from "../constants/actionTypes"
  2.  
  3. const redUserInfo = (state = { 
  4.     userId: 10001, 
  5.     userName: ''
  6.     userOpenid: ''
  7.     userPhone: ''
  8.     userRole: 0 
  9. }, action) => { 
  10.     if (action === undefined) { 
  11.         return state 
  12.     } 
  13.  
  14.     switch (action.type) { 
  15.         case USER_INFO: 
  16.             return { 
  17.                 ...state, 
  18.                 ...action 
  19.             } 
  20.         default
  21.             return state 
  22.     } 
  23.  

第三步,完成store的創(chuàng)建

  1. import {createStore} from 'redux' 
  2. import reducers from '../reducers/index' 
  3.  
  4. let store = createStore(reducers) 
  5.  
  6. export default store 

第四步,獲取用戶信息

  1. //配置代碼,通過connect將組件和store連接起來 
  2. let mapStateToProps = (state) => ({ 
  3.     userInfo: {...state.redUserInfo} 
  4. }) 
  5.  
  6. let mapDispatchToProps = (dispatch) => ({}) 
  7.  
  8. export default connect(mapStateToProps, mapDispatchToProps)(PageClass) 
  9.  
  10. //通過props獲取用戶信息 
  11. this.props.userInfo 

第五步,修改用戶信息

  1. import {switchUser} from '../../redux/actions/userInfo' 
  2.  
  3. switchUser({ 
  4.     userId: 10001, 
  5.     userName: ''
  6.     userOpenid: ''
  7.     userPhone: ''
  8.     userRole: 2 
  9. })(); 

至此就完成了redux+React-redux的一個簡單使用流程

 

責(zé)任編輯:姜華 來源: 前端陽光
相關(guān)推薦

2020-11-24 07:48:32

React

2021-08-10 18:36:02

Express原理面試

2020-10-20 09:12:57

axios核心原理

2022-08-27 13:49:36

ES7promiseresolve

2025-03-05 00:00:00

state變更組件

2025-03-05 00:00:00

ReactstoreUI 更新

2022-04-01 07:52:42

JavaScript防抖節(jié)流

2021-05-08 07:53:33

面試線程池系統(tǒng)

2021-07-26 09:00:08

ReactHooks 項目

2022-10-31 11:10:49

Javavolatile變量

2019-09-23 19:30:27

reduxreact.js前端

2023-11-28 17:49:51

watch?computed?性能

2025-03-05 00:01:00

ReduxReact

2020-07-03 17:20:07

Redux前端代碼

2020-10-15 12:52:46

SpringbootJava編程語言

2023-05-11 07:25:57

ReduxMiddleware函數(shù)

2024-09-25 12:26:14

2024-04-22 09:12:39

Redux開源React

2020-12-18 05:42:46

reduxactions

2015-08-18 08:55:03

redux核心
點贊
收藏

51CTO技術(shù)棧公眾號