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,
}
}
添加回答
舉報