由于我对Redux
的环境还很陌生,因此我遇到了一些教程,我的指导老师针对每个状态创建了多个reducer,最后使用combineReducers
示例将它们组合在一起下面的代码。我想知道为什么必须组合多个reducer来处理大型应用程序
import { createStore, combineReducers } from "redux";
import { Dishes } from "./dishes"; //These are the constant variable from different
import { Comments } from "./comments"; //files where the Reducers are located
import { Promotions } from "./promotions";
import { Leaders } from "./leaders";
export const ConfigureStore = () => { //CREATE the store here
const store = createStore(
combineReducers({ //we show how exactly various reducers combine
dishes: Dishes, //dishes managed by Dishes
comments: Comments,
promotions: Promotions,
leaders: Leaders
//recomposition of global state in our application
})
);
return store; //created the store
}
答案 0 :(得分:1)
combineReducers
使您可以灵活地将状态逻辑分为较小的块或化简器,并专注于该特定化简器中的特定行为或数据。
例如,在上面,您具有菜肴,评论,促销和领导者平价商品。
每个化简器都是独立的,仅关注于处理其自身的数据,而不是在单个化简器中具有复杂的状态,并在单个化简器中汇总每次更新的所有逻辑
还将逻辑分解成多个化简器可以帮助您获得更简单的状态格式,这将使您更轻松地维护更新
它也有助于性能优化,因为如果同一reduce自身存储所有数据,则要更新深度嵌套的状态,您将克隆其余数据,这意味着很多参考比较都会失败