我们采用了以下项目 struct

|- pages
    |- <page_name>
        |- index.js             # To do a default export of the main component
        |- MainComponent.jsx    # Contains other subcomponents
        |- main-component.css   # CSS for the main component
        |- OtherComponents.jsx  # more than 1 file for child components that are used only in that page
        |- __tests__            # Jest unit and snapshot tests
|- components
    |- index.js                 # Exports all the default components of each component as named exports
    |- CommonCmpnt1
        |- CommonCmpnt1.jsx
        |- common-cmpnt-1.css
        |- index.js             # To default export the component
        |- __tests__
    |- CommonCmpnt2
        |- CommonCmpnt2.jsx
        |- common-cmpnt-2.css
        |- index.js             # To default export the component
        |- __tests__

要澄清的是,没有任何东西坏了,而且它运行得非常好.我们在components目录中的多个页面中重用了组件,我们在其中导入如下内容

// Assuming we are inside MainComponent.jsx
// ...
import { CommonCmpnt1 } from '../../components';    // We even take it a step further and use absolute imports

此外,只使用一次的组件并排放置在它的pages文件夹中.

The only problem we face now is热模块重新加载被 destruct ,即每当我们在pagescomponents目录中编辑.jsx文件时,我们必须手动重新加载页面以查看影响的更改(它不会影响CSS文件).这是我们已经习惯的事情,因此不会对我们造成严重影响.

我的问题是,有没有我们不知道的即将到来的灾难?

推荐答案

对于所有仍然对此感兴趣的人,我个人推荐这种方法,因为它有助于划分资源,并允许快速查找内容和单元测试.

它的灵感来自HackerNoon在How to structure your React app上的一篇文章

Reactjs相关问答推荐

如何将google地址lat收件箱设置为输入值?

MUImaterial -ui-如何将文本字段和 Select 分组?

如何在取数后添加新数据,并在页面上保存旧数据?

防止在Reaction中卸载

有没有一种方法可以在没有强烈动画的情况下有条件地渲染组件?

如何在物料界面react 的多选菜单中设置最大 Select 数限制

悬停轴时重新绘制自定义参照线

根据用户 Select 的注册类型更改表单字段

状态更改是否卸载功能组件

Material-UI 和 React Hook 表单不记录值

Redux 工具包不更新状态

在 React 中将 MUI 样式外包到单独的文件中?

使用jest如何覆盖对象的模拟?我正在使用`jest.mock()`来模拟对象,并希望 for each 测试用例覆盖模拟.

如何在next.js 13中仅在主页导航栏上隐藏组件?

setState 改变对象引用

setState 改变对象引用

如何使用 cypress 获取 MUI TextField 的输入值?

如何在 JSX 中使用 -webkit- 前缀?

如何在 react-router-dom v6 中实现监听功能?

ClearInterval 在 React 中的 useRef 没有按预期工作