Priynaka
I am new to react js and I want to learn how to create multiple pages websites in React js here is a sample design I want to make: getcricketidonline.com/ please guide me
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Heya,
Creating a multi-page website in React typically involves using a routing library like React Router. React Router enables you to define multiple routes in your application, each corresponding to a different view or page. Here’s a basic guide on how to get started with a multi-page React application:
If you haven’t already created a React app, you can start one using Create React App:
npx create-react-app my-app
cd my-app
npm start
This command creates a new React application and starts the development server.
React Router is not included in the default React setup, so you need to add it to your project:
npm install react-router-dom
In React, pages are typically represented as components. For a simple multi-page app, you might have a few components like Home, About, Contact, etc. Create these components in your project. For example:
// Home.js
function Home() {
return <h1>Home Page</h1>;
}
export default Home;
// About.js
function About() {
return <h1>About Page</h1>;
}
export default About;
// ... similarly for other pages
In your App.js, import the components and set up routing using BrowserRouter and Routes from react-router-dom:
import React from "react";
import { BrowserRouter as Router, Routes, Route, Link } from "react-router-dom";
import Home from './Home';
import About from './About';
// ... import other pages
function App() {
return (
<Router>
<div>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
{/* ... other links */}
</ul>
</nav>
{/* A <Routes> looks through its children <Route>s and
renders the first one that matches the current URL. */}
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
{/* ... other routes */}
</Routes>
</div>
</Router>
);
}
export default App;
If you haven’t already, start your development server:
npm start
Your application should now be running on http://localhost:3000, and you should be able to navigate between your pages using the links.
As you get more comfortable with React and React Router, you can explore more advanced topics, such as dynamic routing, nested routes, route guards, and handling 404 pages.
Remember, building a multi-page React app can seem daunting at first, but with practice, it becomes much more manageable. Happy coding!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.