简体   繁体   English

使用 React Redux 商店的最佳方式是什么?

[英]What is the best way to use the React Redux Store?

I am using Redux-Thunk for Reactjs project.我正在为 Reactjs 项目使用 Redux-Thunk。 but I am not sure how I have to make a redux store.但我不确定如何制作 redux 商店。 making the redux store for each web page will be good?为每个 web 页面制作 redux 存储会好吗? now I made the stores for each object (maybe it is related to each database table) such as sites, rooms, sensors...现在我为每个 object (可能与每个数据库表有关)例如站点、房间、传感器...

import { siteConstants } from '../_constants';

function sites(state = {}, action) {
    switch (action.type) {
        case siteConstants.GETALL_REQUEST:
            return {
                ...state,
                get : {
                    status : 'loading',
                    data : [],
                }
            };
        case siteConstants.GETALL_SUCCESS:
            return {
                ...state,
                get : {
                    status : 'success',
                    data : action.sites,
                }
            };
        case siteConstants.GETALL_FAILURE:
            return {
                ...state,
                get : {
                    status : 'failure',
                    data : [],
                    error: action.error
                }
            };

        case siteConstants.GET_REQUEST:
            return {
                ...state,
                selected_site : {
                    status : 'loading',
                    data : [],
                }
            };
        case siteConstants.GET_SUCCESS:
            return {
                ...state,
                selected_site : {
                    status : 'success',
                    data : action.site,
                }
            };
        case siteConstants.GET_FAILURE:
            return {
                ...state,
                selected_site : {
                    status : 'failure',
                    data : [],
                    error: action.error
                }
            };

        case siteConstants.ADDNEW_REQUEST:
            return {
                ...state,
                add : {
                    status : 'adding',
                }
            };
        case siteConstants.ADDNEW_SUCCESS:
            return {
                ...state,
                add : {
                    status : 'success',
                },
                get : {
                    ...state.get,
                    data : [
                        ...state.get.data,
                        {
                            _id : action.site._id,
                            name : action.site.name,
                            description : action.site.description,
                            vectorMapUrl : action.site.vectorMapUrl,
                        }
                    ]
                }
            };
        case siteConstants.ADDNEW_FAILURE:
            return {
                ...state,
                add : {
                    status : 'failure',
                    error : action.error,
                }
            };

        case siteConstants.DELETE_REQUEST:
            return {
                ...state,
                delete : {
                    status : 'deleting',
                }
            };
        case siteConstants.DELETE_SUCCESS:
            return {
                ...state,
                delete : {
                    status : 'success',
                },
                get : {
                    ...state.get,
                    data : [
                        ...state.get.data.slice(0, action.index),
                        ...state.get.data.slice(action.index + 1)
                    ]
                }
            };

        case siteConstants.DELETE_FAILURE:
            return {
                ...state,
                delete : {
                    status : 'success',
                    error : action.error,
                }
            };

        case siteConstants.UPDATE_REQUEST:
            return {
                ...state,
                update : {
                    status : 'updating',
                }
            };
        case siteConstants.UPDATE_SUCCESS:
            return {
                ...state,
                update : {
                    status : 'success',
                },
                get : {
                    ...state.get,
                    data : [
                        ...state.get.data.slice(0, action.i),
                        {
                            _id : action.site_id,
                            name : action.name,
                            description : action.description,
                            vectorMapUrl : action.vectorMapUrl,
                        },
                        ...state.get.data.slice(action.i + 1)
                    ]
                }
            };
        case siteConstants.UPDATE_FAILURE:
            return {
                ...state,
                update : {
                    status : 'failure',
                    error : action.error,
                }
            };

        default:
            return state;
    }
}

export default sites;

as you see, current there is only one store for sites CRUD on my web application project.如您所见,目前在我的 web 应用程序项目中只有一个站点 CRUD 商店。 but there are relations between data tables.但是数据表之间是有关系的。 for example例如

  • sites table网站表
  • rooms table will have site id rooms 表将具有站点 ID
  • sensors table will have site id and room id传感器表将具有站点 ID 和房间 ID

for this relation, what is the best store structure for React Redux对于这种关系,React Redux 的最佳存储结构是什么

Redux stores are typically built around the concept of slices, where the entire data set is split into manageable units, with each unit being a property in the store object. Redux 存储通常围绕切片的概念构建,其中整个数据集被分成可管理的单元,每个单元都是存储 object 中的一个属性。 This lets you code each segment one at a time, reducing the amount of code you have to manage in one place.这使您可以一次编写一个代码段,从而减少您必须在一个地方管理的代码量。

There are two schools of thought: sliced by data versus sliced by feature.有两种思想流派:按数据切片和按特征切片。

When a store is set up by data, you gain an advantage by having your data laid out in a cohesive manner, from a top-down perspective.当商店由数据建立时,您可以通过自上而下的角度以连贯的方式布局数据,从而获得优势。 For what you're describing, this makes sense, as you have a clear hierarchical relationship in your data.对于您所描述的内容,这是有道理的,因为您的数据中有明确的层次关系。 Actions can then be defined in a CRUD-like manner.然后可以以类似 CRUD 的方式定义操作。

The one challenge is exactly how you manage different kinds of related data.一个挑战正是您如何管理不同类型的相关数据。 If you have different data in different slices, yet have to maintain ID integrity across data sets, you might end up making multiple dispatches across different slices, which can get tricky.如果您在不同的切片中有不同的数据,但必须保持跨数据集的 ID 完整性,您最终可能会在不同的切片上进行多次调度,这可能会变得很棘手。 You can, of course, keep all your data in one slice, but that can get bloated.当然,您可以将所有数据保存在一个切片中,但这可能会变得臃肿。

When a store is set up by feature, each slice will be related to an activity in your app.当按功能设置商店时,每个切片都将与您应用中的一个活动相关。 This can make more sense if you have different "transactions," such as a retail site having profile management, shopping, and a community support blog.如果您有不同的“事务”,例如具有配置文件管理、购物和社区支持博客的零售站点,这可能更有意义。 Slicing by feature lets you confine update concerns to a single slice, making each type of data associated with the feature more manageable.按功能切片可让您将更新关注点限制在单个切片中,从而使与该功能相关的每种类型的数据更易于管理。 It also lets you put the sliced code for a feature in the same folder as the components for that feature, which makes for a nice comprehensive package.它还允许您将功能的切片代码与该功能的组件放在同一文件夹中,这使得 package 非常全面。

The main challenge with slice-by-feature is deciding where data goes that could be impacted by multiple features.逐个特征的主要挑战是确定可能受多个特征影响的数据去向。 Shared or common folders, or determining a "home" folder, are different ways of managing this.共享文件夹或公用文件夹,或确定“主”文件夹是管理此文件夹的不同方式。

So what's the right answer?那么正确答案是什么? Of course, it depends.当然,这取决于。 A lot has to do with how you want your app to flow.很大程度上与您希望您的应用程序如何流动有关。 Is it heavily siloed?它是否严重孤立? Then features might make sense.那么特征可能是有意义的。 Is it mostly data access?主要是数据访问吗? Then data slices might make sense.然后数据切片可能有意义。

It's likely that you'll end up with a mixture of the two.很可能你最终会得到两者的混合。 You may have a primary "data slice," with additional feature slices.您可能有一个主要的“数据切片”和附加的特征切片。 You might find that one kind of transaction flow makes it easier to manage data integrity than another, such as using a wizard-style entry path versus a free-form "add a node to a tree" style.您可能会发现一种事务流比另一种更容易管理数据完整性,例如使用向导式输入路径与自由格式的“将节点添加到树”式。

One last note: If you are looking to manage slices quickly and with some nice built-in philosophy and features, I heavily recommend checking out Redux Toolkit .最后一点:如果您希望快速管理切片并使用一些不错的内置理念和功能,我强烈建议您查看Redux 工具包 It's made by the same folks who made Redux and implements the best practices they've seen in the wild to make reducer and action creator code manageable and self-documenting.它是由制作 Redux 的同一个人制作的,并实施了他们在野外看到的最佳实践,以使 reducer 和 action creator 代码易于管理和自我记录。

暂无
暂无

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

相关问题 在 React Redux 中实现撤消状态更改(撤消存储/历史实现)的最佳方法是什么 - What is the best way to implement undo state change (undo store/history implementation) in React Redux 将react-redux与基于事件的第三方库结合使用的最佳方法是什么? - What's the best way to use react-redux with event based 3rd party library 存储要与jquery一起使用的数据的最佳方法是什么? - What is the best way to store data to use with jquery? React + Redux - 在表单组件中处理CRUD的最佳方法是什么? - React + Redux - What's the best way to handle CRUD in a form component? 使用 React 和 Redux 处理数据密集型请求的最佳方法是什么? - What is the best way to handle data-intensive requests with React and Redux? 处理 react redux 中的获取错误的最佳方法是什么? - What is the best way to deal with a fetch error in react redux? 写入数据以存储在React-redux中最简单的方法是什么? - What is the simpliest way to write data to store in React-redux? 在 React Native 中使用 Redux 和本地 state 以优化性能的最佳方式? - Best way to use Redux and local state in react native to optimize performance? 将索引 object 添加到 Redux 商店(Redux 工具包)的最佳方法是什么? - What's the best way to add an indexed object to a Redux store (Redux Toolkit)? React - 使用多个 createContexts 的最佳方式是什么? - React - What is the Best Way to use Multiple createContexts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM