简体   繁体   English

Redux状态设计

[英]Redux state design

I'm new to Redux. 我是Redux的新手。 I want to refine my app's state design. 我想完善我的应用程序的状态设计。 Roughly I have 3 pages that users may recognize. 大概我可以识别3个页面。

  • Page 1 is for printing list of articles. 第1页用于打印文章列表。
  • Page 2 is for editing an article. 第2页用于编辑文章。
  • Page 3 is for printing list of appendixes, and editing parameters of each appendix here. 第3页用于打印附录列表,并在此处编辑每个附录的参数。

I have designed it like this. 我是这样设计的。 Does it have no problem? 没问题吗?

{
    articles : [
        {
            id,
            title,
            body,
            appendixIds
        },
        ...
    ],
    isFetchingArticles,
    articlesPagination : {
        pageSize,
        pageIndex
    },
    appendixes : [
        {
            id,
            title,
            body,
            configs
        }
    ],
    isFetchingAppendixes,
    appendixesPagination: {
        pageSize,
        pageIndex
    }
}

State design is often up to interpretation, however, something that can help is thinking about how you will set up your reducers. 状态设计通常取决于解释,但是,可以帮助您思考如何设置减速器的事情。

If you're using combineReducers then the state will be broken up into one object for each reducer you pass into it. 如果您使用combineReducers则对于您传递到其中的每个Reducer,状态将被分解为一个对象。 So for example, if you have one reducer for articles and another for appendices, then the state resulting from using combineReducers would look something like this: 因此,例如,如果您有一个用于文章的减速器,另一个是用于附录的减速器,那么使用combineReducers产生的状态看起来像这样:

{
   articles: {
     ....
   },
   appendices: {
     ...
   }
}

Using this, what could be more beneficial would be to set up your state like this: 使用此方法,可能更有利的是像这样设置您的状态:

{
   articles: {
      items: [
        {
           id: x,
           title: x,
           body: x,
           appendices: x
        },
        ...
      ],
      isFetching: false,
      pagination: {
         size: x,
         index: x
      }
   },
   appendices: {
     ...
   }
}

Also, take a look at normalizr https://github.com/paularmstrong/normalizr . 另外,看看normalizr https://github.com/paularmstrong/normalizr This normalises your state tree, so you could use it for references to other objects like your appendixIds in the articles. 这可以规范化状态树,因此您可以将其用于引用其他对象,如文章中的appendixIds

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

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