In this article, I will explain how to build a simple React Js application. I will also explain how the React Js project structure is organized.
The application is a simple signup form. Hence the project name is signup-form.
The following command creates the project.
Now I have a React Js project named signup-form. Navigate to that project directory:
Start the development server by running the following:
Now I can access my default React Js home page at http://localhost:3000.
Now, let's explore the project structure created by Create React App. The folders public and src and the files index.html,
You may notice files in addition to the files mentioned above. Just ignore them for now. I will explain them later.
In a typical React.js project,
This file is the main HTML file for my application and serves as the React application's entry point. It contains the basic structure of the web page,
including the
<!DOCTYPE html>
<html lang="en">
<head>
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
This file is the main JavaScript file for the application, and it serves as the entry point for React.
It imports the React and ReactDOM libraries and the App component from the
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './index.css';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
)
This file is a React component containing the application's logic and structure. It typically includes a
In summary, index.html provides the basic structure for the web page. The
As you saw in the above explanation,
Let's start by creating a new component for our signup form. Create a new file named
const SignupForm = () => {
return (
<div className="signup-form">
<h1>Signup Form</h1>
<form>
<div className="form-field">
<label htmlFor="name">Name</label>
<input type="text" id="name" name="name" required />
</div>
<div className="form-field">
<label htmlFor="email">Email</label>
<input type="email"id="email" name="email" required />
</div>
<button type="submit">Signup</button>
</form>
</div>
)
}
export default SignupForm
Now I have to import this SignupForm into
Now, open the
import React from 'react';
import SignupForm from './SignupForm';
function App() {
return (
<div>
<SignupForm />
</div>
)
}
export default App
Now I can see the contents of my
The contents of my signup form need styling. They look messy. There are multiple ways to style my React application, such as using CSS files, CSS modules, or styled-components. In this example, I will use a simple CSS file.
Create a new file named
.signup-form {
width: 300px;
margin: 0 auto;
}
.form-field {
display: flex;
flex-direction: column;
margin-bottom: 1rem;
}
input {
padding: 0.5rem;
}
Now, import the CSS file in the
My signup form component should include the form fields, buttons, and styling defined in the SignupForm.css file.
When I run the application using