Skip to main content

React.js File Structure: Beginner's Guide to Your First React Project

5 min read826 words

� What is React.js? React.js is a popular JavaScript library for building user interfaces, especially for websites and web applications. Created by Facebook, React helps you build interactive websites by breaking them into small, reusable pieces called components. Think of it like building with LEGO blocks - each component is a block that you can use to build bigger structures! 🧱⚛️

💻 Creating Your First React Project The easiest way to start a React project is using Create React App. This tool sets up everything you need automatically. Open your terminal or command prompt and run this command:

1 2 3 npx create-react-app testreact cd testreact npm start

🎉 What Happens Next? After running the command: • A new folder called testreact is created • All necessary files are downloaded and set up • Your default browser opens showing your new React app • You'll see a spinning React logo - that's your app running!

� Exploring Your Project Structure When you open your project folder in VS Code or any code editor, you'll see this file structure:

Figure 1: Your React project structure in the file explorer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 testreact/ ├── public/ # Static files (don't edit these much) │ ├── index.html # The main HTML page │ ├── favicon.ico # The small icon in browser tab │ └── manifest.json # App settings for mobile ├── src/ # This is where you'll write your code! │ ├── App.js # Your main component - start editing here │ ├── App.css # Styles for your main component │ ├── index.js # Connects your app to the browser │ └── index.css # Global styles for your whole app ├── .gitignore # Tells Git which files to ignore ├── package.json # Lists all the tools your app needs ├── package-lock.json # Locks specific versions of dependencies └── README.md # Instructions for your project

🏗️ What Each Folder and File Does

📂 public/ Folder: • Contains files that are served directly to the browser • index.html is the main page where your React app appears • You rarely need to edit files here as a beginner • Think of it as the foundation of your house

📂 src/ Folder: • This is your main workspace - where all the magic happens! • App.js is your main component - start editing here • index.js connects your app to the browser (don't touch this yet) • App.css and index.css are for styling your app • All your future components will go in this folder

📄 package.json: Lists all the tools and libraries your app needs to run. Don't worry about this file yet!

📄 package-lock.json: Keeps track of exact versions of all tools your app uses. Never edit this file - it's managed automatically!

📄 .gitignore: Tells Git (version control) which files to ignore when saving your project. This includes node_modules and other files you don't want to share.

📄 README.md: Contains instructions and information about your project.

🎯 Which Files Should You Edit to Get Started? As a beginner, focus on these files: • App.js - This is your main component. Change the text, add new content here • App.css - Style your main component with colors, fonts, layouts • index.css - Add global styles that affect your whole app

✏️ Making Your First Edit Let's make a simple change:

  1. Open src/App.js in your code editor
  2. Find the text that says 'Edit src/App.js and save to reload'
  3. Change it to 'Hello, this is my first React app!'
  4. Save the file
  5. Watch your browser automatically update with the new text! 🎉

� How to Start Your React App To run your React app anytime:

  1. Open terminal in your project folder
  2. Run npm start
  3. Your app opens in the browser at http://localhost:3000
  4. Any changes you make will automatically show up in the browser!

🛑 How to Stop Your React App To stop your React app: • Press Ctrl + C in the terminal where it's running • Or simply close the terminal window

�📁 Adding More Components As you learn more, you'll want to add more components. Here's how to organize them:

1 2 3 4 5 6 7 8 9 src/ ├── components/ # Create this folder for your components │ ├── Header.js # A header component │ ├── Button.js # A reusable button component │ └── Footer.js # A footer component ├── App.js # Your main component ├── App.css # Main component styles ├── index.js # Don't edit this yet └── index.css # Global styles

** Simple File Naming Rules** • Components: Use PascalCase like Header.js, MyButton.jsOther files: Use camelCase like utils.js, helpers.js • Always use .js extension for JavaScript files • Keep names simple and descriptive

✅ Best Practices for Beginners • Start by editing only App.js, App.css, and index.css • Create a components/ folder when you have more than 2-3 components • Keep one component per file • Name your files clearly (Header.js for header component) • Save your work often - React updates automatically! • Don't worry about complex folder structures yet - keep it simple!

� Common Beginner Mistakes • Don't edit files in the public/ folder unless you know what you're doing • Don't delete index.js - your app needs it to work • Don't create too many folders at first - start simple • Don't forget to save your files after making changes • Don't worry if you make mistakes - you can always undo!

🎊 Next Steps Once you're comfortable with the basic structure:

  1. Learn how to create your first custom component
  2. Practice adding CSS styles to make your app look beautiful
  3. Learn about React props to pass data between components
  4. Explore React hooks like useState for interactive features

Remember: Every React developer started exactly where you are now. Take it one step at a time, and you'll be building amazing apps in no time! 🌟

🔗 Helpful Resources

Create React App: https://create-react-app.dev/ - Official documentation • React Documentation: https://react.dev/learn - Learn React step by step • VS Code: https://code.visualstudio.com/ - Best code editor for React • Node.js: https://nodejs.org/ - You need this installed first

Frequently Asked Questions

How do I create my first React project?

To create your first React project, use Create React App by running 'npx create-react-app testreact' in your terminal. This automatically sets up all the files and folders you need to start building your React application.

What files should I edit as a React beginner?

As a beginner, focus on editing three main files: App.js (your main component where you add content), App.css (styles for your main component), and index.css (global styles that affect your whole app). Don't edit files in the public/ folder yet.

What is the src folder in React?

The src/ folder is your main workspace where you'll write all your React code. It contains App.js (your main component), index.js (connects your app to the browser), and CSS files for styling. All your future components will also go in this folder.

What is the public folder in React?

The public/ folder contains static files that are served directly to the browser, like index.html (the main page), favicon.ico (the small icon in the browser tab), and manifest.json (app settings). As a beginner, you rarely need to edit these files.

How do I start and stop my React app?

To start your React app, run 'npm start' in your terminal while in your project folder. This opens your app in the browser at http://localhost:3000. To stop it, press Ctrl + C in the terminal or close the terminal window.

How should I name my React component files?

Use PascalCase for React component files (like Header.js, MyButton.js). For other files, use camelCase (like utils.js). Always use .js extension for JavaScript files and keep names simple and descriptive.

What happens when I save changes to my React files?

When you save changes to your React files, the browser automatically updates to show your changes without needing to refresh the page. This is called 'hot reloading' and makes development faster and easier.

Was this article helpful?

Share this article

Topics covered in this article

Zeeshan Ali profile picture

About Zeeshan Ali

Technical Project Manager specializing in Web/Mobile Apps, AI, Data Science, AI Agents, and Blockchain. Passionate about creating innovative solutions and sharing knowledge through technical writing and open-source contributions.

More Articles by Zeeshan Ali