Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Structure of a React Project | React in Practice
Introduction to React

bookStructure of a React Project

If we look at the project folder, we will find the following files:

The only files of folders that are relevant at this point are:

  • node_modules contains all the installed modules;
  • public contains the main index.html file and other files related to it;
  • src contains a basic template for the HTML page;
  • package.json and package-lock contain data related to the project, the installed modules, and their versions.

We need to start our work by deleting all the files from the src and public folders.

Create an index.html file in the public folder and add the following code:

index.html

index.html

copy

Create an index.js file and add the following code inside it:

import React from "react";
import ReactDOM from "react-dom/client";

function Main () {
    return (
      <ul>
        <li>Cats</li>
        <li>Dogs</li> 
        <li>Birds</li>
        <li>Lizards</li> 
      </ul>
    );
}

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<Main />);

In the index.js file, we import React and ReactDOM to use the React functions.

Note that we don't need to manually import index.js in index.html: it is automatically done for us when we run the application.

We can use the DOM in this script to access the root element from the HTML document and create a root object to render React components. It is done in the second-last line as follows:

const root = ReactDOM.createRoot(document.getElementById("root"));

In the last line, we use the created React root object to render the Main component, which represents a React component. We will learn more about React components in the next section.

root.render(<Main />);

Following is what the output should look like for the above setup:

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 4

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Spørg mig spørgsmål om dette emne

Opsummér dette kapitel

Vis virkelige eksempler

Awesome!

Completion rate improved to 2.7

bookStructure of a React Project

Stryg for at vise menuen

If we look at the project folder, we will find the following files:

The only files of folders that are relevant at this point are:

  • node_modules contains all the installed modules;
  • public contains the main index.html file and other files related to it;
  • src contains a basic template for the HTML page;
  • package.json and package-lock contain data related to the project, the installed modules, and their versions.

We need to start our work by deleting all the files from the src and public folders.

Create an index.html file in the public folder and add the following code:

index.html

index.html

copy

Create an index.js file and add the following code inside it:

import React from "react";
import ReactDOM from "react-dom/client";

function Main () {
    return (
      <ul>
        <li>Cats</li>
        <li>Dogs</li> 
        <li>Birds</li>
        <li>Lizards</li> 
      </ul>
    );
}

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<Main />);

In the index.js file, we import React and ReactDOM to use the React functions.

Note that we don't need to manually import index.js in index.html: it is automatically done for us when we run the application.

We can use the DOM in this script to access the root element from the HTML document and create a root object to render React components. It is done in the second-last line as follows:

const root = ReactDOM.createRoot(document.getElementById("root"));

In the last line, we use the created React root object to render the Main component, which represents a React component. We will learn more about React components in the next section.

root.render(<Main />);

Following is what the output should look like for the above setup:

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 4
some-alt