How to Set Up Vite for a Multi-Page Application?
Last Updated :
11 Sep, 2024
Vite is a frontend development tool, used to build projects and multiple page applications in React.js. It is a beginner-friendly user tool that was developed by the founder of Vue.js, another framework of React.js. we will learn how to set up Vite for a Multi-Page Application.
Steps to Set Up Vite for a Multi-Page Application
Step 1: Create a vite project using the following command:
npm create vite@latest
Step 2: Name your project & select the React framework here using the downward arrow key.
Vanilla
Vue
React
Preact
Lit
Svelte
Solid
Qwik
Others
Step 3: Select Variant: choose any variant of your choice using the downward arrow key,i.e: choose JavaScript.
TypeScript
TypeScript + SWC
JavaScript
JavaScript + SWC
Step 4: Now, switch to my-react-app directory
cd my-react-app
Step 5: Install node dependencies.
npm install
- Now your Vite project has been setup.
- To allow going on different web pages, ie., to create a Multi-page application, we need to install a dependency on our project.
Step 6: Use the following command to install dependencies to allow accessing multiple pages.
npm install react-router-dom
Updated Dependencies:
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.26.1"
},
Project Structure:
Project StructureUse of React-router-dom
React-router-dom is a dependency in Vite that is used to access and create multiple web page applications. A popular term, Routing is used for the same. We use routes in Vite to route, or go to the other web page. For eg. The default route of a website www.abc.com would have a route of '/', and we can create a route for the login page, which would be a part of the same website, but would redirect to a different page with the url www.abc.com/login.
Step 7: Add Browser Router to main.jsx
- In the main.jsx file, add a Browser Router enclosing the entire app.jsx.
- This can also be added directly in the App.jsx file but it should be an enclosing parameter of all the components or routes being used by the project.
- Here, we have added BrowserRouter from react-router-dom to configure the entire directory for routes. In other words, it simply tells the application that now it is ready to become a multi page application.
JavaScript
// main.jsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './index.css';
import { BrowserRouter } from 'react-router-dom';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<BrowserRouter>
<App />
</BrowserRouter>
);
Step 8: Add Routes and Route
- Create a Pages directory in which add three components which you would like to show through your multipage application.
- Here, we have created Contests.jsx, DSA.jsx and Login.jsx.
- Add a bit of code in each one of them that displays the name of the page as heading and a small text in it.
- Now, in your App.jsx file create Routes and specific Route for each component that you want to define in your application. For eg. Here, we want that the '/login' route takes us to the Login.jsx component file, but on a different page. Thus, we will define a Route for the same.
- For eg. We have imported the pages from src/pages/ directory and have defined a Routes component. Within that we have added Login.jsx component.
<Routes>
<Route path="/login" element={<Login />} />
</Routes>
Here are all the components and pages code:
CSS
/* index.css */
body {
margin: 0;
font-family: Poppins;
font-size: large;
}
nav ul {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
}
nav ul li {
margin: 0 10px;
}
nav ul li a {
text-decoration: none;
color: #007bff;
}
JavaScript
// src/Components/Header.jsx
import React from "react";
import { Link } from "react-router-dom";
const Header = () => {
return (
<div>
<div style={{ textAlign: "center" }}>
<h1 style={{ color: "green" }}>GeeksForGeeks</h1>
<p>We are learning routes in Vite project</p>
<nav>
<ul style={{ listStyleType: "none", padding: 0 }}>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/login">Login</Link>
</li>
<li>
<Link to="/dsa">DSA</Link>
</li>
<li>
<Link to="/contests">Contests</Link>
</li>
</ul>
</nav>
</div>{" "}
</div>
);
};
export default Header;
JavaScript
// src/Pages/Login.jsx
import React from "react";
const Login = () => (
<div>
<h1>Login</h1>
<p>This is the Login page of GeeksForGeeks.</p>
</div>
);
export default Login;
JavaScript
// src/Pages/DSA.jsx
import React from 'react';
const DSA = () => (
<div>
<h1>DSA</h1>
<p>This is the DSA page of GeeksForGeeks.</p>
</div>
);
export default DSA;
JavaScript
// src/Pages/Contest.jsx
import React from 'react';
const Contests = () => (
<div>
<h1>Contests</h1>
<p>This is the Contests page of GeeksForGeeks.</p>
</div>
);
export default Contests;
Step 9: Create Navigate function
To navigate to different routes, we use useNavigate. Here, we have created a button within the App.jsx component on clicking which the useNavigate() function is triggered, and we ca specify the route we want to go back to. Here, we have defined a variable of navigate that the function returns and it takes us to the default route, or home page of the application on click.
Here is the final App.jsx file with navigate function and other routes:
JavaScript
// src/App.jsx
import React from "react";
import { Route, Routes, useNavigate } from "react-router-dom";
import Login from "./pages/Login";
import DSA from "./pages/DSA";
import Contests from "./pages/Contests";
import Header from "./Component/Header";
const Button = () => {
const navigate = useNavigate();
const handleClick = () => {
navigate("/");
};
return (
<button
onClick={handleClick}
style={{
padding: "10px 20px",
fontSize: "16px",
cursor: "pointer",
outline: "none",
border: "2px solid green",
borderRadius: "4px",
marginTop: "16px",
marginBottom: "16px",
}}
>
Go to Home
</button>
);
};
const App = () => (
<div style={{ textAlign: "center" }}>
<Header />
<Button />
<Routes>
<Route path="/" element={<div>Welcome to the Home page</div>} />
<Route path="/login" element={<Login />} />
<Route path="/dsa" element={<DSA />} />
<Route path="/contests" element={<Contests />} />
</Routes>
</div>
);
export default App;
Output:
Navigate Function
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read