This repository demonstrates how to build a simple React application with React Router DOM for navigation and routing. The application includes multiple pages such as Home, About, Contact Us, Login, and a protected Dashboard accessible only to authenticated users.
- React Router DOM: Navigate seamlessly between multiple pages without reloading.
- Login Authentication: Basic form validation for username and password.
- Error Handling: Informative error messages for invalid login attempts or empty fields.
- Responsive Design: Adaptable layout for various screen sizes.
- Modular Structure: Each component is well-organized for reusability and scalability.
.
├── src
│ ├── Components
│ │ ├── About.js # About Page Component
│ │ ├── ContactUs.js # Contact Us Page Component
│ │ ├── Dashboard.js # Dashboard Component (protected)
│ │ ├── Home.js # Home Page Component
│ │ ├── Login.js # Login Page Component
│ ├── CSS
│ │ ├── Login.css # CSS for the Login Component
│ ├── App.js # Main App Component with Routes
│ ├── index.js # Application Entry Point
│ ├── App.css # Global CSS
├── public
│ ├── index.html # HTML template
├── package.json # Project Metadata
└── README.md # Documentation (You are here!)
- React.js: Frontend library for building user interfaces.
- React Router DOM: Library for handling client-side routing.
- CSS: Styling the components for a polished UI.
git clone https://github.com/LayuruLK/Using-React-Router-Dom.git
cd myappMake sure you have Node.js installed. Then run:
npm installStart the development server:
npm startOpen your browser and visit: http://localhost:3000
The application includes the following pages:
- Home: General information about the app.
- About: Details about the app and its features.
- Contact Us: A contact form for users to reach out.
- Login: A login form to authenticate users.
- Dashboard: A protected page accessible only after successful login.
- Username:
admin - Password:
123
- Displays a red error message if required fields are empty or credentials are invalid.
Defines the routes for the application using React Router DOM:
<Routes>
<Route path='/home' element={<Home />} />
<Route path='/about' element={<About />} />
<Route path='/contact' element={<ContactUs />} />
<Route path='/login' element={<Login />} />
<Route path='/dash' element={<Dashboard />} />
</Routes>Handles user authentication with form validation and error messages:
const handleSubmit = (e) => {
e.preventDefault();
if (!formData.username || !formData.password) {
setError("Please Fill All Required Fields!");
return;
}
if (formData.username === 'admin' && formData.password === '123') {
navigate('/dash');
} else {
setError("Invalid username or password!");
}
};- Global CSS (
App.css): Basic layout and navigation styling. - Component-specific CSS (
Login.css): Focused styling for the Login page.
.login-form input {
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
.login-form button {
background-color: #007BFF;
color: #fff;
border: none;
padding: 10px 15px;
cursor: pointer;
}Contributions are welcome! Feel free to fork this repository and submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.
Your Name
- GitHub: LayuruLK
Enjoy coding! 🚀