redux簡單說明

2021-09-13 21:06:57 字數 1495 閱讀 6289

redux: 根據actions型別, 操作store的值,

store: 簡單理解成觸發機制的中介軟體,乙個應用只能有乙個store

actions: 提供函式觸發redux裡面的事件。

定義redux:

const initialstate = 

const loggeduserreducer = (state = initialstate, action) => , state, )

} return state

}export default loggeduserreducer

建立store:
import  from 'redux'

import loggeduserreducer from './reducers/loggeduserreducer'

const reducers = combinereducers()

const store = createstore(reducers)

export default store

定義action:
import store from '../store'

export const getloggeduser = () => )

}, 500)

}

元件中使用:

使用redux相關元件時,最外層必須用provider包裹,並傳入store。

//這個是使用redux的元件

通過把元件和redux連線,就可以在元件中使用redux裡面定義的變數。因為combinereduxcer的時候給redux定義的屬性名是loggeduserstate ,因此可以這樣獲取redux裡面的值。

const statetoprops = () => ()

export default connect(statetoprops)(authorizedroute)

定義可以在元件authorizedroute中通過this.props.logged和this.props.pending直接使用redux的值。然後可以直接把actions引入進來,呼叫actions裡面的方法去觸發dispatch。

雖然不知道這樣子用的好處,但是見到就說明一下。這樣子寫就是在actions裡面不使用dispatch,然後通過在元件裡面執行bindactioncreators實現dispatch功能。

import * as userinfoactions from '../actions/actions'

const mapdispatchtoprops = (dispatch) =>

} export default connect(

statetoprops

)(authorizedroute)

元件就可以通過this.props.userinfoactions呼叫需要的actions方法。

FormatMessage簡單說明

常用引數簡介.formatmessage format message from system format message allocate buffer,null,lval,null,lptstr hlocal,0,null 第乙個引數是標誌,告訴這個formatmessage函式,我想要的特點...

Subversion 簡單說明

subversion 簡單說明 checkout 從源 管理伺服器取出 commit 提交更新的 到源 管理伺服器。update 從源 管理伺服器獲取 的最新更新。export 從源 管理伺服器匯出 匯出的 將脫離源 管理。revert 將當前資料恢復到最近與伺服器同步的版本,即放棄自己的所有未提交...

std deque簡單說明

本文 http blog.chinaunix.net u2 86575 showart 1734758.html std deque是乙個高效的雙端佇列,可以高效地進行插入和刪除操作。以sgi c stl為例 deque的基本結構是 有乙個map map中的元素是乙個記錄了乙個大小為512位元組的線...