I'm setting up a React project with my team that will use mobX as state manager, plus TypeScript.
I've seen a common pattern in the casing and naming patterns in React Projects:
- Non-React Folders and Files:
camelCase
orkebab-case
- React (inside
components
folder):PascalCase
Is there a formal convention for folder/file naming in react? If not, is there a style guide on which this pattern is based? Or a reason why this one is used most of the times?
There is not official style guide for React. But you can use most popular
eslint
configuration for React by AirBnb.Read more here https://github.com/airbnb/javascript/tree/master/react
Just to add my two cents. As others have stated, file structure is unopinionated. However, component naming is not. They should be
PascalCase
for React to know whether or not you're using afunction
,class
or anHTMLelement
†.For example:
Bad! Why? Because React doesn't know whether or not you're trying to use the
input
element or the class-based component.That's why you'll see PascalCase components:
† There is one exception, where you can use
dot notation
. For example, if you had multiple exports and you import them all asfields
, then you could do something like:component/fields/index.js
components/App/index.js
As a general rule of thumb, I avoid
dot notation
altogether. It feels clumsy and may confuse other developers who don't know howfields
is structured. Plus, I'm not a fan of stacking multiple components within 1 file and then importing them as a bunch. In addition, the file can become quite large and cumbersome to navigate and debug (more on this below).That said, to keep my structure simple, I like to keep main directories lowercase:
Then within the
component
folder, I'll PascalCase my components to represent something like this:Why this structure?
Input
is self-contained within this folder. Therefore, I can hand it off to someone and they can slot it in their application and just use it.index.js
, so it's very easy to import without traversing a ton of nested files:import Input from 'components/Input';
(also, no need to specify the exactjs
file to use since "index.js" contains all required code).Drawbacks:
index.js
nomenclature so it can be a bit confusing at first as to what "index.js" has failed.Another approach I used to do was:
Why this structure?
Input
is self-contained within this folder. Therefore, I can hand it off to someone and they can slot it in their application and just use it.function
or aclass
.Drawbacks:
import Input from 'components/input/input.js';
Other general guidelines:
Example of a default exported anonymous function:
Why? Because when testing, the function will show up in enzyme as:
When you have multiple anonymous functions within a component, which one is which!?
More often than not, I've found that most components will fall under 100 lines or so when properly optimized. Worst case scenario is I'll have to create small subcomponents to supplement the main component. But! Much easier to read and debug.
What's easier to read:
Example #1 (34 lines with supplemental child components)
Example #2 (318 lines of everything)
Example #1 mimics reading a book. Multiple pages that when glued together create an easy-to-read experience. Versus Example #2 which reads a like a mile-long scroll that can be easy to get lost in!
This one can be confusing, but it all depends on how you're applying styles. If you're just importing the style like so:
Then you can use snake-case:
However, if you're using
css modules
, then you'll need to use camelCase:Why? Because bundlers (like Webpack) don't support snake-case imports:
Conclusion: There are many ways to create a folder structure with a few tips and tricks to maintain a logical flow. Just pick one that works best for you AND doesn't interfere with the person working beside you!
In other words, K.I.S.S === "Keep it simple, silly!"
At the moment i have a Folder named in PascalCase and within it i have an
index.js
file - this is my Component.Any Components directly attached to the root Component i have nested in their own Folder with their own index.js. I also use dot notation to describe the nature of any files directly related to that folder e.g
[descriptor].[name].[prefix]
And for my mobx Store, because i'm less likely to have a real deep folder structure with my store modules i have one Root module-Folder with normally two
js
files within themActions.js
&index.js
index being my main store Class that extends my Actions class. (i found that one mobx class withobservable
,computed
andaction
properties got a bit cluttered).The Store folder itself has an
index.js
which imports all sibling store-modules in-order to combine them into one store Object later on (needed for my project)I suppose there is no REAL right way as it's down to preference, the way above i find readable and when using tools on VSCode to find files it can make it easier when searching for specifics like "i want to see all files that are constants files" searches for
constants.[component name]
It is common across many languages to PascalCase their classes and have camelCase functions and variable names. A JS example,
components are often classes
class Nav extends React.PureComponent
and so the logical connection is to name the file containing the class similarly, resulting in matching case import statementsimport Nav from './Nav
You may also have a utility file, which exports a function, not a class. Again, it's nice to have matching cases
import hello from './hello'
As such, you may find a common structure like
There is no official guide. The reason why most projects adopt PascalCase for react components is to mimic the main export of that file. React components are PascalCased by convention, and when using jsx pascal casing becomes mandatory (only Capitalised first letter becomes mandatory actually). cameCase or kebab-case for the remaining files is just following what is also the more common preference for javascript projects in general.