第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用多個 Reducer 和 Actions Redux 的問題

使用多個 Reducer 和 Actions Redux 的問題

ABOUTYOU 2023-05-25 16:23:42
我有一個小問題。我在不同的文件中使用組合減速器有不同的減速器,但是當我嘗試在這些減速器上使用“不同的”初始狀態(tài)時,它沒有出現(xiàn)例如Product Reducer -> 這是我必須采取的狀態(tài)const INITIAL_STATE = {    productosInventario: [],    loading: false,    error: ''Category Reducer -> 這是這些減速器的狀態(tài)const INITIAL_STATE = {    categorias: [],    categoriaActual: '',    loading: false,    error: ''}這個想法是在這些組件上同時使用:
查看完整描述

1 回答

?
猛跑小豬

TA貢獻1858條經(jīng)驗 獲得超8個贊

const mapStateToProps = (reducers) => {

    return (

        reducers.ProductoReducer,

        reducers.CategoriasReducer

    )

}

看起來你在狀態(tài)和減速器之間有一些混淆。狀態(tài)是包含所有數(shù)據(jù)的對象。它只是一個普通的 javascript 對象。reducer 是一個函數(shù),它接受狀態(tài)對象和一個動作并返回一個新的狀態(tài)對象。


您的設置應如下所示:


const productoReducer = (state = INITIAL_PRODUCTOS, action ) => {

  switch ( action.type ) {

    case 'TRAER_TODOS_LOS_PRODUCTOS':

      /* ... code here ... */

    default:

      return state;

  }

}


const categoriasReducer = (state = INITIAL_CATEGORIAS, action ) => {

  switch ( action.type ) {

    case 'LISTAR_CATEGORIAS':

      /* ... code here ... */

    default:

      return state;

  }

}


export const reducer = combineReducers({

  producto: productoReducer,

  categorias: categoriasReducer,

})

這里我們有兩個單獨的類別和產(chǎn)品縮減器,每個都有一個單獨的初始狀態(tài)。我們過去常常combineReducers將它們放在一起,所以現(xiàn)在組合狀態(tài)具有屬性producto和categorias。


您的組件Inventario需要從狀態(tài)訪問一堆值: categoriasInventario, productosInventario, loading, 和error。我們沒有將狀態(tài)傳遞到組件中,而是使用mapStateToProps提取這些值并將它們作為道具傳遞。


const mapStateToProps = (state) => {

    return {

        categoriasInventario: state.categorias.categorias,

        productosInventario: state.productos.productosInventario,

        loading: state.categorias.loading || state.productos.loading,

        error: state.categorias.error || state.productos.error,

    }

}


查看完整回答
反對 回復 2023-05-25
  • 1 回答
  • 0 關注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號