You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React应用组件化文件夹结构理解困惑与结构咨询

React组件化文件夹结构:你的直觉没错,但需要区分组件类型

Hey Craig, great question—this is a super common pain point when starting out with React, and you’re totally right to sense that shoving all your pages and shared UI elements into a single components directory isn’t the best approach. Let’s break this down:

First: Why your current structure isn’t ideal

Your instinct to group by components makes sense, but you’re mixing two distinct types of components:

  • Reusable UI components (Header, Footer, Navbar): These are pure, presentational elements that get used across multiple pages.
  • Page/Route components (Login, Register, Landing, About): These are the entry points for your app’s routes, and they often assemble multiple UI components into a complete view.

When you lump them all together, your components folder will quickly become bloated. Finding the right file becomes a chore, and it’s hard to distinguish between components that are meant to be reused vs. those that are tied to a specific page.

1. Page + Shared Components Split (Best for Most Projects)

This is the most widely adopted structure because it’s intuitive, scalable, and keeps responsibilities clear. Here’s how it looks:

src/
  |-- pages/          # 所有路由级页面组件
  |    |-- Landing/
  |    |    |-- Landing.jsx
  |    |    |-- Landing.css
  |    |-- Login/
  |    |-- Register/
  |    |-- About/
  |    |-- Contact/
  |-- components/     # 可复用UI组件
  |    |-- Layout/    # 布局相关组件(Header/Footer/Navbar适合放在这里)
  |    |    |-- Header.jsx
  |    |    |-- Footer.jsx
  |    |    |-- Navbar.jsx
  |    |    |-- MainLayout.jsx  # 可选:把Header/Footer/Navbar封装成单个布局组件
  |    |-- UI/        # 基础原子组件(Button、Input、Card等)
  |    |    |-- Button.jsx
  |    |    |-- Input.jsx
  |-- hooks/          # 自定义React Hooks
  |-- utils/          # 工具函数
  |-- routes/         # 路由配置文件

为什么这个结构好用:

  • 页面组件和通用UI组件职责明确,找文件效率极高
  • Layout子文件夹统一管理全站布局,你甚至可以封装MainLayout组件,让所有页面直接复用,不用每个页面重复引入Header/Footer
  • 随着项目增长可以平滑扩展,新增页面或组件不会打乱现有结构

2. 按功能/业务模块拆分(适合大型复杂项目)

如果你的应用有明确的业务领域(比如认证、用户中心、电商商品模块),按功能组织代码能让相关逻辑更内聚。示例:

src/
  |-- modules/
  |    |-- auth/          # 认证功能模块
  |    |    |-- pages/
  |    |    |    |-- Login.jsx
  |    |    |    |-- Register.jsx
  |    |    |-- components/  # 认证专属组件(比如LoginForm、PasswordResetModal)
  |    |-- home/          # 首页模块
  |    |    |-- pages/
  |    |    |    |-- Landing.jsx
  |    |    |-- components/  # 首页专属组件(比如HeroBanner、FeaturedCards)
  |-- shared/             # 全局共享资源
  |    |-- components/
  |    |    |-- Layout/
  |    |    |-- UI/
  |    |-- hooks/
  |    |-- utils/

为什么这个结构好用:

  • 同一功能的所有代码都放在一起,团队协作时不会跨文件夹找相关代码
  • 专注单个功能时,认知负担更低

3. 极简结构(适合小型项目)

如果你的项目很小(只有几个页面),可以保持简洁但仍区分页面和通用组件:

src/
  |-- components/
  |    |-- Header.jsx
  |    |-- Footer.jsx
  |-- pages/
  |    |-- Landing.jsx
  |    |-- Login.jsx
  |-- App.jsx
  |-- index.jsx

给你的具体建议

  1. 把所有页面组件(Login、Register、Landing等)从components移到新建的pages目录
  2. 将Header、Footer、Navbar放到components/Layout/下,考虑封装MainLayout.jsx,通过children属性包裹页面内容,减少重复代码
  3. 随着项目增长,再逐步细化结构(比如新增UI文件夹存放按钮、输入框这类基础组件)

最后补充:React没有“官方唯一正确”的文件夹结构,核心原则是职责单一、便于维护、符合团队共识。只要你的结构能让你(和团队)快速找到文件、逻辑清晰,就是好结构。你的初始直觉是对的,只是需要按组件的用途做拆分。

内容的提问来源于stack exchange,提问作者Craig

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:49:51