Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
File Folder Structure Organization | Styling in React Apps
Mastering React

File Folder Structure Organization

Before proceeding with the styles, we must consider file folder structuring and code organization. In the previous chapter, we touched on this topic. We already know that we need the following:

  • Create a separate folder for each component;
  • Create a jsx file for this component;
  • Create a module.css file for this component;
  • Combine them so the jsx file could know about styles.

What is next? To ensure clarity, we will develop a simple app to comprehend its organization.

Note

Full project code and live page can be found in the end of this chapter.

content

The components comprising our app include:

  • The Bar component represents the app's navigation bar, consisting of a logo and various links;
  • The Information component displays an image and accompanying text content;
  • The Footer component represents the copyright and all rights reserved;
  • The Section component serves as a behind-the-scenes element. It acts as a wrapper, centrally aligning all the content with the assistance of CSS;
  • The App component represents the entire app. We will construct the whole project within the App.jsx file and import it into the main index.js file.
content

Organization

When organizing our files, we follow a structure where each component has its dedicated folder. We create a jsx file within each folder and a corresponding module.css file. This approach leads to the following structure:

content

How does index.js know about all the components?

As previously mentioned, we construct the entire project within the App.jsx file, and subsequently, we import and render that component (App component) in the index.js file.

We import a file with the help of the import statement and the correct path to the file.

Let's create some components to check the syntax.

App component

The App.jsx file is the entry point where we import and assemble all other components to build the project. This entails importing all the necessary components within the App.jsx file and structuring the app accordingly.

You may have observed that file exports are also crucial in addition to file imports. Proper exporting makes it possible to import a component into another file. Hence, including line 17 specifically for the App component serves the purpose of exporting it. This ensures that the App component is available for other parts of the codebase to import and utilize within the application. The syntax is:

Section component

It serves as app wrapper. It gets children prop and render them in the div element. The div has some styles that were added with the help of Section.module.css file.

Section.jsx code:

Section.module.css code:

Bar component

The Bar component renders a specific markup and applies corresponding styles. The overall organization of this process remains consistent. We start by importing the CSS file associated with the component, then proceed to create the component itself, where we render the necessary markup and apply class names to achieve the desired styling. Finally, we export the component, ensuring its availability in other parts of the codebase.

Bar.jsx code:

Bar.module.css code:

Note

To avoid overwhelming the chapter, we will not delve into a detailed analysis of the other components. This is because the process followed for each component is precisely the same. Instead, I encourage you to examine the complete project provided below thoroughly. Take the time to inspect each folder and open each component to understand its organization. This is crucial because as we progress further, additional components will be added. It is important that we do not overlook any details at this stage.

Full app code:

Note

To examine the project root, click on the slider on the left side of the interface to expand it, and in the top-left corner, you will find a burger button (represented by three horizontal lines). Clicking on the burger button will provide access to the project root, allowing you to explore the files and folders within the project.

Everything was clear?

Section 2. Chapter 9
course content

Course Content

Mastering React

File Folder Structure Organization

Before proceeding with the styles, we must consider file folder structuring and code organization. In the previous chapter, we touched on this topic. We already know that we need the following:

  • Create a separate folder for each component;
  • Create a jsx file for this component;
  • Create a module.css file for this component;
  • Combine them so the jsx file could know about styles.

What is next? To ensure clarity, we will develop a simple app to comprehend its organization.

Note

Full project code and live page can be found in the end of this chapter.

content

The components comprising our app include:

  • The Bar component represents the app's navigation bar, consisting of a logo and various links;
  • The Information component displays an image and accompanying text content;
  • The Footer component represents the copyright and all rights reserved;
  • The Section component serves as a behind-the-scenes element. It acts as a wrapper, centrally aligning all the content with the assistance of CSS;
  • The App component represents the entire app. We will construct the whole project within the App.jsx file and import it into the main index.js file.
content

Organization

When organizing our files, we follow a structure where each component has its dedicated folder. We create a jsx file within each folder and a corresponding module.css file. This approach leads to the following structure:

content

How does index.js know about all the components?

As previously mentioned, we construct the entire project within the App.jsx file, and subsequently, we import and render that component (App component) in the index.js file.

We import a file with the help of the import statement and the correct path to the file.

Let's create some components to check the syntax.

App component

The App.jsx file is the entry point where we import and assemble all other components to build the project. This entails importing all the necessary components within the App.jsx file and structuring the app accordingly.

You may have observed that file exports are also crucial in addition to file imports. Proper exporting makes it possible to import a component into another file. Hence, including line 17 specifically for the App component serves the purpose of exporting it. This ensures that the App component is available for other parts of the codebase to import and utilize within the application. The syntax is:

Section component

It serves as app wrapper. It gets children prop and render them in the div element. The div has some styles that were added with the help of Section.module.css file.

Section.jsx code:

Section.module.css code:

Bar component

The Bar component renders a specific markup and applies corresponding styles. The overall organization of this process remains consistent. We start by importing the CSS file associated with the component, then proceed to create the component itself, where we render the necessary markup and apply class names to achieve the desired styling. Finally, we export the component, ensuring its availability in other parts of the codebase.

Bar.jsx code:

Bar.module.css code:

Note

To avoid overwhelming the chapter, we will not delve into a detailed analysis of the other components. This is because the process followed for each component is precisely the same. Instead, I encourage you to examine the complete project provided below thoroughly. Take the time to inspect each folder and open each component to understand its organization. This is crucial because as we progress further, additional components will be added. It is important that we do not overlook any details at this stage.

Full app code:

Note

To examine the project root, click on the slider on the left side of the interface to expand it, and in the top-left corner, you will find a burger button (represented by three horizontal lines). Clicking on the burger button will provide access to the project root, allowing you to explore the files and folders within the project.

Everything was clear?

Section 2. Chapter 9
some-alt