Structure d'un projet React.js

Untangling the Mystery of React Project Structure: npm init, Your First Jutsu

Welcome, young React padawans and aspiring Jedi Masters, to an unprecedented journey through the organized yet sometimes mysterious labyrinth of a React project’s structure! Grab your coffee (or tea, let's be inclusive) because we’re diving headfirst into what makes up the backbone of our favorite React applications.

The Trio of Magical Folders

It all starts with a bit of black magic… or rather a flick of npm init react-app (I’m team npm, but no shade to the Yarn crew, they’re cool too). Abracadabra, and boom, three folders appear:

  • node_modules The treasure chest, holding all the gems (modules) needed for your app. Don’t get lost in there!
  • public : The storefront of your shop. This folder contains everything your visitors will see and touch directly, like images, the favicon, and that good old index.html file where all your React magic gets injected.
  • src : The secret workshop where everything is created. This is where components come to life, styles intertwine, and the real development happens.

On ne conseillera jamais assez de jeter un coup d’oeil à la doc: https://react.dev/learn/start-a-new-react-project

The Den of Code: The src Folder

This is where you'll spend your days (and probably nights)… (I do both). The files index.js and App.js are the ringmasters, orchestrating the appearance of all other components on stage. Think of App.js to dress up your components, and index.js for a bit of global style.

As for performance and testing, don’t sweat it, React has got you covered with files like App.test.js for testing, and reportWebVitals.js to monitor your app’s health.

The Guardians of the Temple: Root Files

Here, sacred manuscripts like .gitignore, package.json, and README.md tell the tale of your project to anyone daring to peek. These files are crucial so that everyone, including your future self, understands what’s going on.

Customization: The Art of Organization

Customizing your project is like decorating your apartment. React suggests two schools of thought: feature-based or type-based. For instance, creating a cozy corner for each feature or a dedicated space for each type of element.

We could talk about this for hours. Where I put things and how and why… I dig the type-based structure, grouping files by type, putting all components in a components folder—it’s easier to navigate, avoids duplication but… it might separate pieces of a particular feature from each other… Basically, it’s like tidying your room, better put things where you’ll find them. So... Gryffindor or Slytherin?

Components, Components Everywhere!

Every component, from Nav.js to Footer.js, tells a little story. For example, our Nav is the welcoming host with its links, while Promo shouts out the best deals of the day. Nav est l’hôte accueillant avec ses liens, tandis que Promo vous crie les meilleures offres du jour.

And components can multiply quickly, like a Konoha ninja using multi-shadow clone technique.

All Orchestrated Under JSX

And now, a little spotlight on JSX and that famous className! JSX is this weird dialect that looks a lot like HTML, used to write your React application’s user interface more easily. It’s a syntactic extension for JavaScript, but don’t be fooled: even though it looks like HTML, it’s definitely JavaScript running behind the curtains!

Why className instead of class? Simply because class is a reserved keyword in JavaScript. JSX adopted className to avoid syntactic conflicts, allowing your code to stay clean and organized. ClassName or class, the CSS still sees a . Period. IYKYK.

The Mystery of the Virtual DOM Unveiled

The Virtual DOM is a crucial concept in React. Think of it as a blueprint of your application, a lightweight and speedy copy of the real DOM. When a change occurs, React first updates this virtual DOM. Then, it compares this new virtual DOM with the old one to determine the most efficient way to apply these changes to the real DOM. This minimizes direct DOM manipulations, which can be costly in performance terms, keeping your app fast and responsive.

The Virtual DOM is kind of like Hermione’s Time-Turner. It adjusts the past (the previous DOM) to optimize the present, all without disturbing your application’s space-time continuum.

A Structure Not Imposed, But Proven

There you have it, you survived the guided tour of a React project’s structure! Keep in mind that none of this is set in stone. React is flexible, just like this structure. Npm init react-app helps to start, lays the foundation, but don’t hesitate to tweak it to your flavor.

Remember, in the world of React, even if sometimes folders and files seem to have their own will, it’s up to you to master this organized chaos to create applications that will dazzle the world (or at least your boss).

So, ready to become the master of your own React destiny? The adventure is just beginning!

Leave a comment

Your email address will not be published. Required fields are marked *