【问题标题】:Fractal Project Structure with React and Redux - pros/cons [closed]使用 React 和 Redux 的分形项目结构 - 优点/缺点 [关闭]
【发布时间】:2016-11-02 23:33:41
【问题描述】:

我想知道在 React + Redux 项目中使用 分形结构 的优缺点是什么,我想知道是否有人对这种方法有任何经验,或者是否存在哪些陷阱从文档中看不到。

(分形结构) 也称为:自包含应用程序、递归路由层次结构、提供程序等

上下文: 我正在查看react-redux-starter-kit,它建议使用fractal structure 来组织文件夹。如果我理解得很好,这种方法需要按功能组织项目文件夹并递归嵌套路由。

所以,如果我有一个“事件”资源在哪里

  • /events 列出所有事件
  • /events/new 显示用于插入新事件的表单
  • /events/1/details 显示有关事件的详细信息 编号 1

从样板开始,我必须添加新的路由文件夹,例如:

├── src                      # Application source code
│   ├── main.js              # Application bootstrap and rendering
│   ├── components           # Reusable Presentational Components
│   ├── containers           # Reusable Container Components
│   ├── layouts              # Components that dictate major page structure
│   ├── static               # Static assets (not imported anywhere in source code)
│   ├── styles               # Application-wide styles (generally settings)
│   ├── store                # Redux-specific pieces
│   └── routes               # Main route definitions and async split points
│       ├── index.js         # Bootstrap main application routes with store
│       ├── Root.js          # Wrapper component for context-aware providers
~       ~
│       ├── Events           # Fractal route
│       │   ├── index.js     # Route definitions and async split points
│       │   ├── components   # Presentational React Components
│       │   ├── container    # Connect components to actions and store
│       │   ├── modules      # Collections of reducers/constants/actions or single DUCK module
│       │   └── routes       # Fractal sub-routes (** optional) <-------------
│       │       │
│       │       └── New
│       │       │   ├── index.js     # Route definitions and async split points
│       │       │   ├── assets       # Assets required to render components
│       │       │   ├── components   # Presentational React Components
│       │       │   ├── container    # Connect components to actions and store
│       │       │   ├── modules      # Collections of reducers/constants/actions or single DUCK module
│       │       │   └── routes       # Fractal sub-routes (** optional) <-------------
│       │       │
│       │       └── Details
│       │           ├── index.js     # Route definitions and async split points
│       │           ├── assets       # Assets required to render components
│       │           ├── components   # Presentational React Components
│       │           ├── container    # Connect components to actions and store
│       │           ├── modules      # Collections of reducers/constants/actions or single DUCK module
│       │           └── routes       # Fractal sub-routes (** optional) <-------------
~       ~
│       └── NotFound         # Capture unknown routes in component
~

NewDetails 文件夹嵌套在根 Event 文件夹下。

文档强调了这些主要优点:

  • 它比平面目录结构更好地扩展,文件夹用于 组件、容器等。
  • 可以将路由捆绑到“块”中 使用 webpack 的代码拆分和合并算法
  • 由于逻辑是自包含的,因此可以轻松地将路由分解为单独的 存储库并使用 webpack 的 DLL 插件引用以实现灵活, 高性能开发和可扩展性。

【问题讨论】:

  • 嘿@NickGnd 你对 react-redux-starter-kit 的体验如何?
  • 1.5 年内未发现的杀手锏。这可能是我下一个项目的结构。

标签: javascript reactjs architecture redux


【解决方案1】:

我可以说的一个缺点是无法at a glance 查看所有可用的路线及其各自的组件,这可以通过制作一些描述性的 cmets 来缓解,但它确实增加了路线配置的复杂性。我还会尝试将嵌套文件夹保持在最低限度,因为在导入语句中获得正确的嵌套级别会产生认知负担,即../../../../../ 如果您有许多嵌套路由,这些可能会失控。

【讨论】:

  • 您可以使用 webpack 或 babel 插件为模块起别名。为您的顶级应用程序目录设置一个别名,并始终从那里导入。
【解决方案2】:

我遇到的类似结构的一个缺点或缺点是,如果/当开始在其层次结构之外使用事物时,您必须在导入中使用大量 ../../..

例如,假设您要求在 StartPage 路由上显示最新事件的详细信息。

所以现在看起来像:

routes
├─Events
│     ├─New
│     ├─Details
├─StartPage
       ├─ components   // here somewhere you import ../../Events/Details

这不是世界末日,但你漂亮的层次结构不再那么严格了。

【讨论】:

  • 感谢您的回答。您可以使用 webpack resolve.root 配置来避免每次导入时使用 ../../..。请参阅docsthis 关于 SO 的问题
  • 还有 'babel-plugin-module-resolver',如果你想让它在后端也能工作的话。
  • 当使用 resolve.root 或 babel 的模块解析器插件时,我认为你会失去编辑器(或其他代码分析工具)可以提供的很多好处,例如能够直接跳转到从另一个文件导入的函数的定义/声明。
  • @Markus-ipse 我会挑战你的结构,说也许你应该有一个更高的组件类,它位于 Events 和 StartPage 路由目录之外,并且它们都使用相同的组件。这毕竟是 React 的要点之一:可重用组件。我对正确结构的想法是,如果多个路由使用相同的组件,则将其移出到更高级别的组件目录。只有特定于其路由的组件才应该驻留在路由的目录层次结构中。
  • 当今的 IDE 工具自动格式化导入语句。我再也不用担心../../../了。 (我在 VS Code 中)。
猜你喜欢
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-07
  • 1970-01-01
相关资源
最近更新 更多