避免在redux存储中复制嵌套对象

时间:2017-09-01 19:59:31

标签: redux reducers redux-orm

我正在尝试redux-orm我可以收集的内容,redux-orm为每个注册的模型在商店中创建一个对象,其关键是通过require __DIR__ . '/vendor/autoload.php'; 指定的值。

好吧,创建我的"实体" reducer,我使用 combineReducers ,为我的所有实体传递reducers,如下所示:

Model.name

enter image description here

最后,这就是我使用import { combineReducers } from 'redux'; import City from './cityReducer'; import Poi from './pointOfInterestReducer'; const entitiesReducer = combineReducers({ City, Poi }); export default entitiesReducer; 创建rootReducer的方式:

enter image description here

问题在于, 我认为 ,使用combineReducers我在我的商店中复制了像我这样的密钥如下所示:

enter image description here

你知道我怎么能避免这种情况,让我所有的模特都成为"实体的直接后代"?

类似combineReducers而不是entities>City

编辑:cityReducer.js

entities>City>City

1 个答案:

答案 0 :(得分:0)

您可以通过两种方式使用Redux-ORM来定义其表格"表格#34;结构,并写减速器逻辑来使用这些表。我在博文Practical Redux, Part 1: Redux-ORM BasicsPractical Redux, Part 2: Redux-ORM Concepts and TechniquesPractical Redux, Part 5: Loading and Displaying Data中提供了这两种方法的示例。 (请注意,这些帖子涵盖了Redux-ORM 0.8的使用,0.9版本有一些API更改。我在Practical Redux, Part 9中列出了这些更改。)

第一种方法是编写附加到模型类的reducer逻辑,并使用Redux-ORM ORM实例生成一个reducer,用于创建"表"并为您管理它们。每the example on the front of the Redux-ORM repo

import {createReducer} from "redux-orm";
import {orm} from "./models";

const rootReducer = combineReducers({
    entities: createReducer(orm)
});

另一种方法是编写自己的函数来创建初始状态,可能还有其他处理更新的函数:

import {orm} from "./models";

const initialState = orm.getEmptyState();

export default function entitiesReducer(state = initialState, action) {
    // handle other actions here if desired
    return state;
}

换句话说,不要定义单独的每个型号"表"你自己。让Redux-ORM在自己的reducer中自动定义它们,或者使用它来为实体reducer生成初始状态。

相关问题