React
  • Introduction
  • Getting Started
    • Introduction
    • Before you get started
  • 1. Fundamentals
    • Introduction
    • Rendering
      • JSX
      • Exercise
      • Solution
    • Components
      • Stateless
        • Exercise
        • Solution
      • Stateful
        • Exercise
        • Solution
      • Styling
        • Exercise
        • Solution
        • Using CSS Modules
    • Folder Architecture
  • 2. Intermediate
    • Lifecycle methods
    • Controlled and Uncontrolled components
    • Anti-patterns
    • Refs and the DOM
    • Lifting State Up
  • 3. Advanced Topics
    • Conventions
    • Reconciliation
    • Performance Optimizations
      • Avoiding Reconciliation
      • PureComponent
      • Avoiding inline lambdas
      • Development vs Production build
    • Context
  • 4. Advanced Patterns
    • Higher-order Components
    • Children as Function
    • Renderless Components
    • Portals
    • Error handling
  • Exercises
    • Introduction
    • 1. ProductList light
      • Step 1
      • Step 2
      • Step 3
      • Step 4
      • Extra
      • Solution
Powered by GitBook
On this page
  • Resources
  • TL;DR;
  • index.js?
  • Don't worry, we got you covered
  1. 1. Fundamentals

Folder Architecture

PreviousUsing CSS ModulesNextLifecycle methods

Last updated 6 years ago

Shopify follows a pattern called the Component Folder Pattern to write component. This pattern helps us keep our code base scalable as we go and we add new components.

Resources

  • Writing Scalable React Apps with the Component Folder Pattern

  • The 100% correct way to structure a React app (or why there’s no such thing

  • CLI utility for quickly creating new React components

  • Atom package to rename index.js files to their parent directory names

TL;DR;

From:

src/
|- App.js
|- Title.js
|- Title.css
|- ProductList.js
|- ProductList.css
|- ProductItem.js
|- ProductItem.css

To:

src/
|- App.js
|- Title/
    |- index.js
    |- Title.js
    |- Title.css
|- ProductList/
    |- index.js
    |- ProductList.js
    |- ProductList.css
|- ProductItem/
    |- index.js
    |- ProductItem.js
    |- ProductItem.css

index.js?

There is two thing to highlight here: 1. First, we have a file named index.js. This allows us to keep our import simple. We will still be able to do: import ProductList from './ProductList' from another App.js for instnace. Webpack will automatically load the index.js file. 2. Second, the index.js file contains only a single line of code: export {default} from './ProductList'. It's a weird synthax, I know, but it basically exports the default export from ProductList to be the default export of the current file. 🤓

Why not write your component in index.js?

The problem with this, is that there is a little side effect. A very annoying side effect:

  • All the tabs of your editor will be named index.js

  • When you will search for your component file, you will end up having a hard time finding your file.

This is basically why we chose to favor having this "dumb" index.js file.

Don't worry, we got you covered

At this point you might be wondering why we don't put all the code of ProductList in index.js. If you read about the Component Folder Pattern, you will see that some people actually write the content of their component inside index.js (See for instance - Writing Scalable React Apps with the Component Folder Pattern).

If on your own project you still want to write the content of your Component in index.js, joshwcomeau wrote a to show the name of the folder when you open an index.js file. But because at Shopify we work at scale and because everyone work with different editors, we can't ask everyone to install an editor plugin.

Because it is still a pain in the 🍑 to create a folder and this index.js file each time you want to create a new component you can use a that generates everything for you.

https://medium.com/styled-components/component-folder-pattern-ee42df37ec68
https://hackernoon.com/the-100-correct-way-to-structure-a-react-app-or-why-theres-no-such-thing-3ede534ef1ed
https://github.com/joshwcomeau/new-component
https://github.com/joshwcomeau/nice-index
Donavon West's article
cool Atom plugin
simple command line tool
Atom tab