redux 的簡單實用案例

2021-10-08 07:01:55 字數 1891 閱讀 9744

檔案目錄如下:

整體**如下

/**

* 這是 action 的建構函式

*/const

sendaction=(

)=>()

module.exports =

;

conat rootreducer

(state, action)

=>

;

• 第乙個引數是預設狀態,我們可以定義乙個初始化的 state,然後進行賦值

const initstate =

const

rootreducer

=(state = initstate , action)

=>

;

/**

* 這個檔案是 建立 reducer 函式的,專門用來處理 傳送過來的action

*/const initstate =

const

reducer

=(state = initstate, action)

=>

, state, action)

: state;

}module.exports =

import

from

"redux"

;const store =

createstore

(reducer)

;

import

from

'redux'

;//匯入我們自己建立好的 reducer

import

from

'./../reducer'

;// 構建 store

const store =

createstore

(reducer)

;export

default store;

this

.unsubbscribe = store.

subscribe((

)=>

);

handleclick()

=>

;

整體**如下

import react,

from

'react'

;import store from

'./store'

;import

from

'./action'

;export

default

class

extends

component

componentdidmount()

)});

}render()

>點我,傳送乙個action<

/button>

<

/div>

<

/>)}

}

redux的簡單使用

redux是乙個狀態管理工具,一般在多互動,多資料來源的時候使用redux編寫程式的時候方便很多,結構也更加清晰。以下是廣為流傳的redux工作流程圖 以上的流程我個人理解是 是乙個修改store資料的乙個流程圖,首先元件中的某個操作需要修改store的資料,然後就要通知store,元件就先將要修改...

redux 案例購物車

1,環境我不搭建了,直接寫!import react from react import from react redux import from redux import as actions from actions.js import shopitem from shopitem calcto...

簡單使用redux

import from redux 用於建立倉庫 import from redux const initialstate const add to cart add to cart const cartreducer function state initialstate,action break...