[英]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/invoices
, www.example.com.au/customers
和www.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.js
, invoices.js
和suppliers.js
,写自己在减速器他们每个人以及他们在合并到一个单一的大店index.js
文件中使用combineReducers()
从法redux
Basically the flow is, 基本上就是
index.js
). index.js
)。 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.suppliers
或state.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: 一些技巧:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.