react專案中整合redux

2021-10-04 19:37:00 字數 3832 閱讀 9857

npm install redux

npm install react-redux

npm install redux-thunk

生成store

在專案初始化預設index檔案中設定./index.js

import

from

'redux'

const store =

createstore

(fn)

匯入reducer

import

from

'redux'

import index from

'./main/reducers/index'

import page1 from

'./main/reducers/page1'

const storetree =

combinereducers()

export

default storetree;

在需要redux的前端頁面使用,從ui元件生成容器元件

import from 'react-redux'

const list =

connect

( mapstatetoprops,

mapdispatchtoprops

)//mapstate負責輸入邏輯,將state對映到ui元件的引數(props)

//mapdispatchtoprops負責輸出邏輯,將使用者對ui元件的操作對映為action

mapstatetoprops()

函式:建立乙個從(外部state到props)的對映關係

const

mapstatetoprops

= state =>

}

mapdispatchtoprops

用來建立 ui 元件的引數到store.dispatch方法的對映。也就是說,它定義了哪些使用者的操作應該當作 action,傳給 store

在專案初始化預設index檔案中設定./index.js

讓容器元件拿到state

import

from

'react-redux'

import

from

'redux'

import reducerfile from

'./reducers'

let store =

createstore

(reducerfile)

render

(>

>

<

/provider>

document.

getelementbyid

('root'

))

//根目錄index.js檔案

import react from

'react'

;import reactdom from

'react-dom'

;import

'./common/assets/style/style.css'

;import

*as serviceworker from

'./serviceworker'

;//redux引入檔案和模組

import storetree from

'./store'

;import

from

'react-redux'

;import

from

'redux'

;import thunk from

'redux-thunk'

;import

from

'react-router-dom'

;const store =

createstore

(storetree,

(thunk));

reactdom.

render

(>

>

<

/router>

<

/provider>

, document.

getelementbyid

('root'))

;// unregister() to register() below. note this comes with some pitfalls.

// learn more about service workers:

serviceworker.

unregister()

;

//根目錄下新建reduxstore.js

import

from

'redux'

import index from

'./main/reducers/index'

import page1 from

'./main/reducers/page1'

const storetree =

combinereducers()

export

default storetree;

// ../constants/index.js

export

const

settest

="settest"

;

// ../actions/index.js

import

from

'../constants/index'

export

const

settest

=(test)

=>)}

}

// ../reducer/index.js

import

from

'../constants/index'

const

init_state

=export

default

function

setportlist

(state=

init_state

, action)

default

:return state

}}

// pages/index.js

import

from

'react-redux'

import

from

'../actions/index'

const

mapstatetoprops

= state =>

}@connect

(mapstatetoprops,

)class

index

extends

component

const

=require

('customize-cra');

module.exports =

override

(fixbabelimports

('import',)

,adddecoratorslegacy()

,);

在專案中整合測試

使用小規模測試降低風險 開發人員編寫 和缺陷 tdd是在專案中整合測試最簡便的方式 單元測試不是萬能藥 使用多種測試技巧 確定每個團隊成員在測試中的角色 測試人員稱職嗎 一流的測試人員具有足夠的創造力,編碼工作之前,就能評估系統的設計和架構 編寫 過程中,測試人員會設計和實現他們的測試元件 會衡量測...

iOS專案中整合ijkplayer框架

1 首先需要配置編譯環境,需要安裝homebrew,git,yasm 需先安裝ruby環境 ruby e curl fssl brew install git brew install yasm 2 開始編譯ijkplayer 本質上是對ffmpeg的編譯 cd ijkplayer master i...

redux在react專案中的應用

今天想跟大家分享一下redux在react專案中的簡單使用 1 1.redux使用相關的安裝 2yarn add redux 3 yarn add react redux 連線react和redux 67 2.redux在專案中的基礎使用 89 1.在index.js入口檔案注入store 10 i...