React_4_Notes__Client_Side_Routing_and_React_Router
React_4_Notes__Client_Side_Routing_and_React_Router
Agenda
The browser sends the request for the page linkedin.com. The server
returns a React Bundled file.
behaviour of SPA
1. For this kind of web apps
a. The URL changes but the app doesn't reload
b. You need to have different routes for different page
2. For the end user
a. It should look like an app
b. load time should be small
3. Let us say the browser is sending a request for the Job so it
fetches the latest React bundle. But here exists a situation that
we must remember
4. When we request data from the backend there are always two
components UI and data.
5. Initial Request for UI and Bundle: When the user accesses the
application, the browser sends a request for the initial UI and a
minimal bundle of the application.
6. Initial UI and Placeholder/Loader: The application loads an initial
UI, which may include placeholders or loaders for various
components that are not immediately visible. These
placeholders help create a better user experience by giving the
impression that the application is responsive.
7. User Interaction: When the user interacts with the application,
such as clicking a button or navigating to a specific page, the
application checks whether the necessary UI components for
that action are already loaded.
8. Conditional Data Request: If the UI components for the
requested action are already loaded, the application only makes
a request to the backend for the data needed for that action,
rather than fetching the entire UI and bundle again. Initial few
pages are loaded to avoid unnessary loading again and again on
the user side.
9. Initially we will fetch a few UIs of the pages and when the user
clicks on any buttons, it will simply request data if the page UI
already exists.
10. If you don't optimize it up to a level, the bundle size can be
very big.
3.
4. install module react-router-dom using npm i react-router-dom
5. import BrowserRouter from 'react-router-dom' in the main.jsx or
the page to the lowermost component.
6. Wrap <App /> inside <BrowserRouter></BrowserRouter>
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App.jsx";
import "./index.css";
import { BrowserRouter } from "react-router-dom";
ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>
);
title: Link
1. There is another tag called Link in "react-router-dom". It takes a
prop to="/Home" where we can give some path and it will change
the URL and the Page accordingly.
2. On clicking these buttons, the page won't reload. The content of
the page changes but the page doesn't reload.
Complete code
import { Routes, Route, Link } from "react-router-dom";
function About() {
return <h2>About Page</h2>;
}
function Home() {
return <h3>I am Home Page</h3>;
}
function Listing() {
return <h3>I am Listing Page</h3>;
}
function PageNotFound() {
return <h3>Page Not found</h3>;
}
function Routing() {
return (
<>
<h1>Routing Example</h1>
<nav>
<ul>
<li>
<Link to="/">Home Page </Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/listing">Listing</Link>
</li>
</ul>
</nav>
<Routes>
<Route path="/" element={<Home></Home>}></Route>
<Route path="/about/*" element={<About></About>}>
{" "}
</Route>
<Route path="/listing" element={<Listing></Listing>}></Route>
<Route path="*" element={<PageNotFound></PageNotFound>}>
{" "}
</Route>
{/* path -> /* -. wild card */}
</Routes>
</>
);
}
export default Routing;
function Users(props) {
console.log(props.isAdmin);
let params = useParams();
const userID = params.id;
console.log("param", params);
Before rendering we will just check if the user data is not null then we
will print the user data else we will provide some placeholder like
loading....
function Users(props) {
// console.log(props.isAdmin);
let params = useParams();
let [user, setUser] = useState(null);
console.log(params);
// https://fakestoreapi.com/users/2
useEffect(() => {
async function fetchData() {
const resp = await fetch(`https://fakestoreapi.com/users/${params.id}`);
const userData = await resp.json();
console.log(userData);
setUser(userData);
}
fetchData();
}, []);
return (
<>
{user == null ? (
<h3>...loading</h3>
) : (
<>
<h4>User Name: {user.username}</h4>
<h3> Name: {user.name.firstname + " " + user.name.lastname}</h3>
<h4> Phone: {user.phone}</h4>
</>
)}
</>
);
}
All the component should have their own file. It's just for explanatory
purposes that we have defined everything under the same file.
Project Description
We will be creating an IMDB clone, where we will fetch Real-time
trending movies data and will show it in grid format, we will be
designing the whole application by using Tailwind CSS
1. The user will be able to view all the latest & trneding movies
(IMDB API)
2. User can create his own separate watchlist
3. User can filter movies according to genre
4. User can sort movies according to ratings
5. Pagination will be implemented to move from one page to
another to get updated data of other movies
6. Search feature will be there for movies.
7. We will be deploying this to netlify
What is tailwind css?
Tailwind CSS is a highly popular and innovative utility-first CSS
framework for web development. Unlike traditional CSS frameworks
that provide pre-designed components, Tailwind CSS focuses on
providing a comprehensive set of utility classes that make it easier to
create custom and responsive designs without the need for writing
custom CSS.
Open your terminal, and navigate to the folder where you want to build
your project – for example Desktop. Input the command below in the
terminal and click enter:
cd movies-app
This command will navigate to your project folder. You should have
this:
Inputing "cd movies-app" in terminal to navigate to the "movies-app"
folder
],
theme: {
extend: {},
},
plugins: [],
}
1. @tailwind base
a. This directive adds Tailwind's basic foundational styles to
your project. These are essential styles that provide a
consistent styling baseline across all browsers, similar to
what a CSS reset does.
2. @tailwind components
a. This injects any predefined component styles from
Tailwind. These can include styles defined by Tailwind and
any additional styles from plugins you might be using.
3. @tailwind utilities
a. This directive adds utility classes that Tailwind provides.
Utility classes are the core of Tailwind's design system,
allowing you to style elements directly in your HTML by
applying utility classes that represent specific CSS
properties.
@tailwind base;
@tailwind components;
@tailwind utilities;
Your index.css file contains some default styling. You can clear all that
and paste the three lines of directives above.
Step 8
Testing tailwind
function App() {
return (
<h1 className="text-3xl font-bold underline text-center">Hello world!</h1>
);
}
Cheatsheet - https://nerdcave.com/tailwind-cheat-sheet
Homework
Build a navigation bar and have two LINKS on it, one by the name
Home and one by the name Watchlist
When you click on Home on the Navbar Home component should get
rendered and when you click on Watchlist so Watchlist should get
Rendered