Basic Structure of ReactJs

Having the proper structure in a react project helps you organize the codebase, allow scalability, code readability and it’s a general good practice to have a good structure not just dumping all your code in one folder.

 

Now when we first create our react app with npx command, the following structure is what we see:

Reactjs introduction 1

 

We’re going to edit the structure into this by deleting some files and cleaning the app.js:

Reactjs introduction 2

 

Now that is the base structure of our code, throughout this the structure would change but this our starting point. Today, we are going to be adding one folder to our structure which is components.

 

Components  are one of the essential key parts/features of a react project, they are the building blocks of react applications which are created to allow modularity and reusability in a react project.

State is another feature of react, it is react’s  special way of managing data across components.

 

Now back to our project we’re going to create our first component and place it in the right place:

Reactjs introduction 3

 

From the above we’ve created a component directory where all subsequent components will be placed for now and later on some abstractions will be made.

 

Now inside our Example.js we have the following:

Reactjs introduction 4

 

This display the text but we will have to import this into App.js so it can be seen it in our application:

Reactjs introduction 5

 
run yarn start in the terminal to open the application in the browser:

 

Reactjs introduction 6
Reactjs introduction 7


And with that you’ve created your first component. Next time we will be talking about react and state

Subscribe For Daily Updates

100+ Python Pattern Examplespython pattern examples - star patterns, number patterns