Getting Started with React: Project Setup and Component Separation

Getting Started with React: Project Setup and Component Separation

How to Think in React

React is a powerful tool for building user interfaces, but to use it effectively, you need to change the way you think about your code. In this guide, we’ll talk about how to break down your project into smaller parts and organize them in a way that makes sense.


Step 1: Setting Up Your React Project

Before you start coding, you need to set up your project. The easiest way to do this is with a tool called create-react-app. Here’s how to get started:

  1. Create a New Folder: Name your folder using lowercase letters, such as my-react-app.
  2. Open the Folder in Visual Studio Code: Navigate to the newly created folder and open it in VS Code.
  3. Run the Command: Inside the terminal of VS Code, run the following command to create a new React application in the current directory:

npx create-react-app .        

2. Start the development server: To see your app in the browser, run this command in terminal,

npm start        


This will open a new window in your browser showing your React app.

Article content

Step 2: Component Separation

One of the key ideas in React is component separation. Before you write any code, think about how to break your user interface (UI) into smaller pieces called components. Each component should do one thing well and be easy to reuse.

Article content


Creating Folders for Project

  1. Create a folder structure: Inside the src folder, create two new folders:

  • layouts: This folder is for larger sections of your app, like LeftMenu and HomeContent.
  • components: This folder is for smaller parts: Logo, MenuTitle, MenuItem, inside the LeftMenu.
  • Common Folder inside the layouts and components folders: You can also create a common folder for parts that you will use in many places, like Section Titles or Section Wrappers. This way, you don't have to write the same code again.

Best Practice:

  • Always name your folders in lowercase with camel case for readability. For example, use navBar instead of Navbar.
  • For JavaScript files, use PascalCase for the first letter of each word (e.g., HomeContent.js).
  • When you create a new React project, it’s important to use lowercase letters for the folder name. This is a best practice that helps maintain consistency and avoids any potential issues with case sensitivity, especially in different operating systems.


Step 3: Creating Layouts and Components


Step 3.1: Creating Layouts

  • Layouts are made up of multiple components. For example, you can create a layout called LeftMenu.js that includes other components:


Article content

In this example, LeftMenu combines smaller components like Logo and MenuItem.


Step 3.2: Creating Components

  • Components are small, reusable parts of your UI. Inside the components folder, create files like Logo.js and MenuItem.js:


Article content

Step 3.3: Creating a Common Folder

For parts that you use often, like buttons or input fields, create a common folder. For example:

components/common/SectionTitle.js


Article content

Step 4: Importing Components into Your App

Now that you have your layouts and components set up, you can bring them together in your Home.js into main app.js file.


Article content
Article content

Step 5: Organizing Your Project

Here’s how your project structure should look,

Article content

  • Layouts: Larger sections that combine multiple components.
  • Components: Smaller parts that do one job.
  • Assets: folder for static files served from the server to the browser.
  • Common folder inside the components and layouts: Reusable components used in different places.


Conclusion

Thinking in React is about breaking your user interface into smaller, manageable parts. By organizing your code into layouts, components, and a common folder, you create a clean structure that is easy to maintain. This approach not only helps you build scalable applications but also simplifies the process of designing complex layouts that may require frequent changes.

With component separation, you can easily modify or replace individual components without affecting the rest of your application. This flexibility allows you to adapt to new design requirements quickly, making React a powerful tool for developing dynamic user interfaces.

Start by setting up your project and thinking modularly as you build your app. With practice, you’ll find that React makes it easier to create complex UIs with less effort. Happy coding!

To view or add a comment, sign in

More articles by Shashika Jayathilaka

Insights from the community

Others also viewed

Explore topics