亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

使用多個 Reducer 和 Actions Redux 的問題

使用多個 Reducer 和 Actions Redux 的問題

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

1 回答

?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

const mapStateToProps = (reducers) => {

    return (

        reducers.ProductoReducer,

        reducers.CategoriasReducer

    )

}

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


您的設置應如下所示:


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,

})

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


您的組件Inventario需要從狀態訪問一堆值: categoriasInventario, productosInventario, loading, 和error。我們沒有將狀態傳遞到組件中,而是使用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 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號