简体   繁体   English

多页的React-Redux状态形状

[英]React-Redux State Shape for multiple pages

I'm trying to design an admin panel website using redux and react that performs basic crud operations on REST resources. 我正在尝试使用redux设计一个管理面板网站,并对REST资源执行基本的crud操作。 I think I'm starting to understand redux, but I'm not quite sure how to structure the state shape, in particular when storing different data per page. 我想我已经开始理解redux了,但是我不太确定如何构造状态形状,特别是在每页存储不同的数据时。 For example the login page will store different data than the page to edit a user for example. 例如,登录页面将存储与页面不同的数据以编辑用户。

I came up with the following "schema" for my state shape: 我为我的状态形状提出了以下“模式”:

var schema = {
    //Things applicable to the authenticated used
    authentication: {
        authenticated: true, //True if a user is authenticated
        auth_user_id: 2, //ID of the authenticated user
    },
    //Things applicable to the layout of the application, for example the notification menus that can be toggled on and off
    layout: {

    },
    //Things specific to each page of the application
    pages: {
        //Login page (eg https://example.com/login)
        login: {
            isFetching: false, //Is data loading?

        },
        //Data pertaining to the page that shows list of users (eg https://example.com/users)
        userList: {

        },
        //Data pertaining to the page to edit a particular user (eg https://example.com/users/:id/edit)
        userEdit: {

        }
    },
    //Normalized entities are stored here.
    entities: {
        usersById: {
            2:
                { //User ID
                    id: 2,
                    first_name:
                        "Some",
                    last_name:
                        "User"
                }
        }
    }
};

I would then have a reducer for each "page" in the pages tree. 然后我会在pages树中为每个“页面”设置一个reducer。 Is this the correct way to handle this? 这是处理这个问题的正确方法吗? I can't seem to find any examples or tutorials on how to structure a larger application that isn't just a simple page that toggles between a couple of views. 我似乎无法找到任何关于如何构建更大的应用程序的示例或教程,这些应用程序不仅仅是在几个视图之间切换的简单页面。

TL;DR / Summary: Yes , your solution seems consistent with general data architectural norms for applications with significant user interface components. TL; DR /摘要: 是的 ,您的解决方案似乎与具有重要用户界面组件的应用程序的通用数据架构规范一致。 However , you may also want to consider some internal component state as well. 但是 ,您可能还需要考虑一些内部组件状态。

This is a general problem in data structures and software architecture, particularly around user interfaces. 这是数据结构和软件架构中的一般问题,特别是在用户界面周围。 There is no one-size-fits-all answer for this; 对此没有一个通用的答案; it will always depend on the specifics of the application and the data on which it operates. 它将始终取决于应用程序的具体情况以及它运行的数据。 I can attest that I have worked professionally on multiple projects (including a current React project, as well as Angular and Backbone) with data structures similar to the one you describe -- ie a mixture of both view-centric and normalized data. 我可以证明我在多个项目(包括当前的React项目,以及Angular和Backbone)上的专业工作,其数据结构与您描述的数据结构类似 - 即以视图为中心和标准化数据的混合。

Early on I encountered the question developing Java Swing applications, where the question was framed as component (view) hierarchy (aka containment hierarchy ) vs. object ( data ) hierarchy (not to be confused with class or inheritance hierarchy) -- and when the latter (data) should mirror the former (views) or not. 早期我遇到了开发Java Swing应用程序的问题,其中问题被构建为组件(视图)层次结构(也称为包含层次结构 )与对象( 数据 )层次结构(不要与或继承层次结构混淆) - 以及何时后者(数据)应该反映前者(观点)与否。

I now more generally think of the problem as designing the data hierarchy with the view hierarchy in mind. 我现在更普遍地将问题视为设计具有视图层次结构的数据层次结构。

Some reasons for having portions of the data hierarchy (in the top-level state/store) mirror the view hierarchy : 使部分数据层次结构(在顶级状态/存储中)镜像视图层次结构的一些原因:

  • Data is very view-specific, representing things on a screen, in natural language, or from user input 数据非常特定于视图,表示屏幕上的内容,自然语言或用户输入
  • Data will not be reused or shared by other areas of the application 数据不会被应用程序的其他区域重用或共享
  • Data represents ephemeral UI-only state, perhaps cached for a few minutes but not permanently 数据表示短暂的仅UI状态,可能缓存几分钟但不是永久性的

Some reasons for having portions of the data hierarchy more normalized and unrelated to view hierarchy: 使部分数据层次结构更加规范化且与视图层次结构无关的一些原因:

  • Reduction of duplication ( DRY principle ) 减少重复( DRY原则
  • Data will be reused or shared 数据将被重用或共享
  • Data represents longer-term state not so closely tied to UI, perhaps duplicated in browser storage 数据表示与UI不太紧密相关的长期状态,可能在浏览器存储中重复

With Redux in particular, some data is so ephemeral or transient that it does not belong in the top-level state/store at all. 特别是对于Redux,一些数据是如此短暂或短暂,以至于它根本不属于顶级状态/存储。 This data could be kept in component state, which also keeps the store more slim and focused. 这些数据可以保持在组件状态,这也使商店更加苗条和专注。

Some reasons for having portions of the data local to component state : 将部分数据置于组件状态的本地化的一些原因:

  • Data is 100% view-specific 数据是100%视图特定的
  • Data should be hidden/ encapsulated from other areas of the application; 数据应该从应用程序的其他区域隐藏/ 封装 ; perhaps it is for a higher-order component , or a component that is generic and highly reusable (the component logic itself -- not the data) 也许它适用于更高阶的组件 ,或者是通用且高度可重用的组件(组件逻辑本身 - 而不是数据)
  • Data represents strictly ephemeral or transient UI-only state 数据严格表示短暂的或瞬态的仅UI状态

Interesting blog entry with extended analysis of using top-level store state vs. component-level state: 有趣的博客条目,包括使用顶级商店状态与组件级状态的扩展分析:

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

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