简体   繁体   English

与Redux互动,如何设计状态

[英]React with Redux, how to design my state

My website has 3 pages, eg www.example.com.au/invoices , www.example.com.au/customers and www.example.com.au/suppliers . 我的网站有3页,如www.example.com.au/invoiceswww.example.com.au/customerswww.example.com.au/suppliers

In this case, how to organize the global Redux state, I mean the structure. 在这种情况下,我是指如何组织全局Redux状态。 should it look like below? 看起来像下面吗?

state = {
    invoices: {
        //invoices related data
    },
    customers: {
        //customers related data
    },
    suppliers: {
        //suppliers related data
    }
}

That looks a good start. 这看起来是一个好的开始。 I find it's a good idea to try and keep the state as normalized as possible - try thinking about it as a relational database. 我发现尝试保持状态尽可能规范化是一个好主意-尝试将其视为关系数据库。

This is a great article: https://medium.com/javascript-scene/10-tips-for-better-redux-architecture-69250425af44#.9c678jwib 这是一篇很棒的文章: https : //medium.com/javascript-scene/10-tips-for-better-redux-architecture-69250425af44#.9c678jwib

as is http://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html 就像http://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html

It is up to you on how to store. 如何存储取决于您自己。 But a more convenient way to do is, having separate file for each of them and combine them as a single store. 但是更方便的方法是为每个文件使用单独的文件,并将它们组合为一个存储。

ie, your folder structure looks some what like this, 也就是说,您的文件夹结构看起来像这样,

reducers/
    |---- index.js
    |---- customers.js
    |---- invoices.js
    |---- suppliers.js

Inside each of customers.js , invoices.js and suppliers.js , write their own reducers in each of them and combine them to a single large store in the index.js file using combineReducers() method from redux 里面每个customers.jsinvoices.jssuppliers.js ,写自己在减速器他们每个人以及他们在合并到一个单一的大店index.js文件中使用combineReducers()从法redux


Basically the flow is, 基本上就是

  • Write separate small-small reducers. 分别编写小型-小型减速器。
  • Import all the reducers in a single file( index.js ). 将所有reducer导入一个文件( index.js )。
  • Using combineReducers() combine them into a single large reducer and export it to store. 使用combineReducers()将它们合并为一个大型reducer,并将其导出到存储。

Hope it helps! 希望能帮助到你! :) :)

state = {
    invoices: {
        //invoices related data
    },
    customers: {
        //customers related data
    },
    suppliers: {
        //suppliers related data
    }
}

this looks good. 这看起来不错。

If you're on invoices page and if you won't be accessing state.suppliers or state.customers , then you don't need to combineReducers all of them together. 如果您在“ invoices页面上,并且不会访问state.suppliersstate.customers ,则无需将所有combineReducers都组合在一起。

You can lazy load reducers (dynamically and use store.replaceReducer to inject them into store). 您可以延迟负载减少器(动态地并使用store.replaceReducer将它们注入到商店中)。 This saves you some bytes ;) 这为您节省了一些字节;)

Some tips: 一些技巧:

  • Separate business logic from views (components) and reducers. 将业务逻辑与视图(组件)和简化器分开。
  • Identify core reducers (those which are needed throughout the app) and others. 确定核心异径管 (整个应用中需要的那些)和其他。 Only create initial store with core reducers. 仅使用核心减速器创建初始存储。
  • Split reducers and action creators into different files and lazy load reducers as required. 将reducer和动作创建者分成不同的文件,并根据需要将懒惰的load reducer拆分为不同的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM