📖 Scenario: You are building a simple React app that shows a list of books. To keep your project neat and easy to grow, you want to organize your files well.
🎯 Goal: Create a basic React folder structure with separate folders for components, pages, and data. Then add a simple component and a data file following this structure.
📋 What You'll Learn
Create a data folder with a file books.js exporting an array of book objects
Create a components folder with a BookList.jsx component
Create a pages folder with a Home.jsx page component
Import and use BookList in Home and pass the books data as props
💡 Why This Matters
🌍 Real World
Organizing files well in React projects makes it easier to work with others and maintain the app as it grows.
💼 Career
Understanding folder structure best practices is a key skill for frontend developers working on React applications in teams.
Progress0 / 4 steps
1
Create the books data file
Create a file books.js inside a folder named data. In it, export a constant array called books with these exact objects: { id: 1, title: 'React Basics' } and { id: 2, title: 'Advanced React' }.
React
Hint
Use export const books = [...] to share the data.
2
Create the BookList component
Inside a folder named components, create a file BookList.jsx. Write a React functional component called BookList that accepts a books prop and returns an unordered list (<ul>) with each book's title in a list item (<li>).
React
Hint
Use books.map to create list items for each book.
3
Create the Home page component
Inside a folder named pages, create a file Home.jsx. Import React, the BookList component from ../components/BookList.jsx, and the books array from ../data/books.js. Write a React functional component called Home that returns a <main> element containing <h1>Book List</h1> and the BookList component with the books prop set to the imported books array.
React
Hint
Remember to import both BookList and books with correct relative paths.
4
Complete the React app structure
In the root App.jsx file, import React and the Home component from ./pages/Home.jsx. Write a functional component called App that returns the Home component. Export App as default.
React
Hint
Use export default function App() and return the Home component inside.
Practice
(1/5)
1. What is the main reason to organize React files into folders like components, pages, and hooks?
easy
A. To make the app run faster
B. To reduce the file size of the app
C. To keep the project easy to understand and maintain
D. To avoid using React hooks
Solution
Step 1: Understand folder organization purpose
Folders like components and hooks group 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 C
Quick Check:
Folder organization = easier maintenance [OK]
Hint: Folders group related files for clarity and ease [OK]
Common Mistakes:
Thinking folder structure affects app speed
Confusing file size with organization
Believing hooks folder is optional for React hooks
2. Which of the following is the correct way to organize a React project folder for components?
easy
A. Put all components in a single components folder
B. Scatter components randomly in the root folder
C. Put components inside the public folder
D. Put components inside the node_modules folder
Solution
Step 1: Identify standard folder for components
React projects usually keep components in a dedicated components folder.
Step 2: Avoid incorrect placements
Components should not be in public or node_modules folders, which serve other purposes.
Final Answer:
Put all components in a single components folder -> Option A