React application used as the base structure for all projects in this guide.
| Package | Version |
|---|---|
| React | 19.2.3 |
| React DOM | 19.2.3 |
| React Scripts | 5.0.1 |
| Testing Library | 16.3.1 |
# Install dependencies
npm install
# Start development server
npm start
# Create production build
npm run build
# Run tests
npm testbase-react-app/
├── public/
├── index.html
├── favicon.ico
├── manifest.json
└── robots.txt
├── src/
├── assets/
| ├── images/
| | ├── basic.jpg
├── components/
| ├── UI/
| | ├── Card.css
| | ├── Card.js
| ├── Footer.css
| ├── Footer.js
| ├── Header.css
| ├── Header.js
├── App.js
├── index.css
├── index.js
├── .gitignore
├── package.json
├── readme.md
The index.js file:
// import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);// import React from 'react';
- Imports the core React library, which is needed to use JSX (JavaScript XML) and create React components.
- Starting from release 17, however, you no longer need this statement.
import ReactDOM from 'react-dom/client';
ReactDOMis a library that interacts with the DOM to render React components.react-dom/clientis a new way (starting from React 18) to manage rendering in React apps, replacing ReactDOM.render
import './index.css';
- Imports the global CSS file for the app.
import App from './App';
- Imports the
Appcomponent fromApp.js, which serves as the root component of the application. - The
Appcomponent is typically where the main structure and logic of the app reside.
const root = ReactDOM.createRoot(document.getElementById('root'));
- This selects the root DOM element with the
id="root"(defined inpublic/index.html) and creates a React root using ReactDOM.createRoot. - The
rootserves as the entry point for the React application, allowing React to manage the DOM within this element. - In the
index.htmlfile, this is the target DOM element where the entire React app will be mounted:<div id="root"></div>
root.render(<App />);
- Renders the
Appcomponent inside therootelement in the DOM. - The
<App />syntax is JSX, representing a React component.
The summary, the index.js component:
- Sets up the environment.
- Renders the top-level
Appcomponent into therootDOM element.
The App.js file:
function App() {
return (
<div>
<h2>Let's get started!</h2>
</div>
);
}
export default App;function App()
- Defines a functional component called
App. - A functional component is one of the simplest ways to define a React component. It is essentially a JavaScript function that returns JSX (HTML-like syntax).
return ( ... )
- The
returnstatement specifies what theAppcomponent renders on the screen. - In this case, it returns a
<div>element containing an<h2>element.
<div> and <h2>
- These are JSX elements that look similar to HTML but are actually JavaScript objects under the hood.
- JSX allows you to write markup directly within your JavaScript code and is then transformed into React elements during compilation.
export default App;
- This exports the
Appcomponent as the default export of the file. - Allows other files to import
App(inindex.jsas importAppfrom'./App';) and use it in the application. - In a larger app, the
Appcomponent would usually serve as the container for other components, managing and rendering their content.
In summary, the App.js component:
- Defines a simple functional React component.
- Renders an HTML structure using JSX.
- Is exported for use as the root component of the app.
Tip
Check out the React app usage documentation for information on basic app usage in this project.