Organizing your React project files clearly helps you and others find code easily. It makes your app easier to build and fix.
Folder structure best practices in React
src/
components/
Button.jsx
Header.jsx
pages/
Home.jsx
About.jsx
hooks/
useFetch.js
utils/
formatDate.js
App.jsx
index.jsxsrc/ is the main folder for your React code.
Group similar files like components, pages, hooks, and utilities into separate folders.
components/.src/components/Button.jsx src/components/Header.jsx
pages/ to separate them from small components.src/pages/Home.jsx src/pages/About.jsx
hooks/ to keep logic reusable and organized.src/hooks/useFetch.js
utils/.src/utils/formatDate.js
This example shows a simple React app with a clear folder structure. Components like Button are in components/. Pages like Home are in pages/. The App component uses the Home page, and index.jsx renders the app.
/* Folder structure example for a React app */ // src/components/Button.jsx import React from 'react'; export function Button({ children, onClick }) { return <button onClick={onClick}>{children}</button>; } // src/pages/Home.jsx import React from 'react'; import { Button } from '../components/Button'; export function Home() { return ( <main> <h1>Welcome to Home Page</h1> <Button onClick={() => alert('Clicked!')}>Click Me</Button> </main> ); } // src/App.jsx import React from 'react'; import { Home } from './pages/Home'; export function App() { return <Home />; } // src/index.jsx import React from 'react'; import { createRoot } from 'react-dom/client'; import { App } from './App'; const container = document.getElementById('root'); const root = createRoot(container); root.render(<App />);
Keep folder names simple and meaningful.
Use index files to simplify imports if your project grows.
Consistent structure helps new team members understand your code faster.
Organize React files by purpose: components, pages, hooks, utils.
Clear structure makes your app easier to build and maintain.
Start with a simple structure and adjust as your app grows.
Practice
components, pages, and hooks?Solution
Step 1: Understand folder organization purpose
Folders likecomponentsandhooksgroup related files by their role.Step 2: Recognize benefits of clear structure
This grouping makes the project easier to read and maintain for developers.Final Answer:
To keep the project easy to understand and maintain -> Option CQuick Check:
Folder organization = easier maintenance [OK]
- Thinking folder structure affects app speed
- Confusing file size with organization
- Believing hooks folder is optional for React hooks
Solution
Step 1: Identify standard folder for components
React projects usually keep components in a dedicatedcomponentsfolder.Step 2: Avoid incorrect placements
Components should not be inpublicornode_modulesfolders, which serve other purposes.Final Answer:
Put all components in a singlecomponentsfolder -> Option AQuick Check:
Components folder = centralized components [OK]
- Placing components in
publicfolder - Scattering components without structure
- Modifying
node_modulesfolder
src/
components/
Button.jsx
hooks/
useToggle.js
pages/
Home.jsx
Which file would you expect to find the custom React hook?
Solution
Step 1: Understand folder naming conventions
Custom hooks are usually placed in thehooksfolder.Step 2: Match file to folder purpose
The fileuseToggle.jsinsidehooksmatches the custom hook naming pattern.Final Answer:
src/hooks/useToggle.js -> Option DQuick Check:
Custom hooks in hooks folder = useToggle.js [OK]
- Looking for hooks in components folder
- Confusing pages folder with hooks
- Assuming index.js contains hooks
src/
components/
Header.jsx
utils/
helpers.js
Header.jsxWhat is the main problem here?
Solution
Step 1: Identify duplicate file names
There are twoHeader.jsxfiles: one incomponentsand one in root.Step 2: Understand confusion caused by duplicates
This duplication can confuse developers and cause import errors.Final Answer:
DuplicateHeader.jsxfile in root and components folder causes confusion -> Option BQuick Check:
Duplicate filenames = confusion [OK]
- Thinking utils folder is bad practice
- Believing components belong in utils
- Ignoring file extensions importance
Solution
Step 1: Understand feature-based organization
Grouping by feature means each feature folder holds all related files, making it easier to find and update.Step 2: Compare with other structures
Flat or type-only folders become hard to manage as app grows, causing confusion.Final Answer:
Organize by feature: each feature folder contains components, hooks, and styles -> Option AQuick Check:
Feature folders = scalable and maintainable [OK]
- Using one big folder causes clutter
- Separating only by file type slows feature updates
- Mixing all files in root is confusing
Ready to prove it?
Pass the test and this pattern moves unknown
