Introduction
In our webpage we will have different options such as about us, contact, more info.
When we click such option what we want to do is , we want to open another page.
One way of doing this is by using anchor tags.
But there is a significant problem in doing so
The disadvantages of using anchor tags are as follows:
Using anchor tags, or
<a>
tags, for navigation within a single-page application (SPA) is considered bad practice because it results in a full page reload.When a user clicks on an anchor link, the browser makes a new request to the server, which then sends back a new HTML page. This process can be slow and can cause the user to lose their current state in the application.
When we make too many requests to the server the following issues can occur:
Slower response times: When a server receives too many requests at once, it may take longer to process each request, resulting in slower response times. This can cause a poor user experience for the client, as they may have to wait a long time for pages or resources to load.
Server overload: If a server receives too many requests at once, it may become overloaded and unable to respond to all the requests. This can result in server crashes, downtime, or errors, which can cause further issues for the client.
Increased server costs: If a client is making too many requests to a server, it can result in increased server costs, as the server may need to use more resources to handle the load.
Security issues: If a client is making too many requests to a server, it may be seen as suspicious behavior and could trigger security measures such as IP blocking, rate limiting, or other types of security measures.
Single Page Applications (SPA)
Single-page applications (SPAs) are web applications that load a single HTML page and dynamically update the content on that page as the user interacts with the application. SPAs use client-side rendering, meaning that the browser loads and renders the HTML, CSS, and JavaScript files necessary for the application to run.
Instead of loading separate HTML pages for each view, SPAs use JavaScript to dynamically render the content of the page, allowing the user to navigate between different views without a full page reload. This provides a faster and more seamless user experience, as the application can update specific parts of the page without needing to reload the entire page.
SPAs often make use of frameworks like React, Angular, or Vue.js, which provide powerful tools for building complex, interactive user interfaces. They also make use of browser APIs like the History API and the Fetch API to manage the state of the application and retrieve data from servers without requiring a page reload.
Using SPAS has some drawbacks as well but the positives overcome the negatives
Types Of Routing
Client-side routing
is when the routing is handled entirely on the client-side, usually using JavaScript frameworks like React or Angular. When the user clicks on a link, the browser doesn't make a request to the server for a new page, instead, the JavaScript framework changes the URL in the browser and updates the view to show the appropriate content. This means that the application loads faster because there is no need to make a round-trip to the server every time the user clicks on a link.Server-side routing
is when the routing is handled by the server. When the user clicks on a link, the browser makes a request to the server for a new page, and the server sends back the appropriate content. This means that the application can be easily crawled by search engines because each page has its unique URL.
React-Router-Dom
React Router DOM is a library for routing in React applications. It provides a declarative way to navigate between different views or pages of a web application without triggering a full page reload.
React Router DOM is built on top of the React Router library and provides a set of components that can be used to define the routes for an application.
Some of the key components provided by React Router DOM include:
BrowserRouter: A component that uses HTML5 history API to keep the UI in sync with the current URL.
Route: A component that renders a UI component when the URL matches its path.
Switch: A component that renders only the first matching Route or Redirect component.
Link: A component that provides a declarative way to navigate to different routes.
Redirect: A component that redirects to a different route.
React Router DOM also provides a range of hooks, such as useHistory, useParams, useLocation, and useRouteMatch, that can be used to access and manipulate the current URL and route parameters.
By using React Router DOM, developers can create a single-page application with multiple views, and provide a seamless user experience without having to reload the page for every view change.
React-Router-Dom-Components
BrowserRouter
is a component provided by thereact-router-dom
library in React that allows for declarative routing in a single-page application.Inside the
BrowserRoute
we pass in an array of paths.These paths are objects
Each of these objects have two fields
path ---- the actual path / url
element ---- what element should it load
errorElement--- what component to load in case of error
RouterProvider
All router objects are passed to this component to render your app and enable the rest of the APIs.Now we want the corresponding link to open when we click it for this we use
<Link to>
Some more points about
<Link to>
<Link to>
is a component in some JavaScript libraries and frameworks, such as React and React Router, that is used for client-side routing.It creates a link to a specific route in the application, and when clicked, it updates the URL in the browser without triggering a full page reload.
This allows the application to provide a smoother user experience, as the content of the page can be updated dynamically without requiring a full page refresh.
Nested routing
is a technique used in web applications where child routes are nested within parent routes.It allows for more complex routing structures where each nested route is a subset of the parent route, and can be defined and managed independently of the parent route.
What if we want to keep the header always while we change the route,this is nested routing
Outlet
is a component such as React Router, that is used for rendering nested child routes within a parent route. It is essentially a placeholder where the child components are rendered based on the current route.We need to specify the path as a children of the header layout as
React-Router-Dom-Hooks
useRouteError
: gives us access to the error in the routeuseParams
: TheuseParams
hook is a React Router hook that allows you to extract parameters from the current route. It is used to access dynamic parameters from the URL, such as/:id
, and pass them as props to components.
:id
In a route pattern,
:id
is a dynamic parameter that can represent any value in the URL. Theid
is just a variable name that is used to identify the parameter and can be any valid JavaScript identifier.When a URL with a dynamic parameter is accessed, React Router extracts the value of the parameter from the URL and passes it to the component as a prop with the same name as the parameter.
For example, if you have a route pattern like
/users/:id
, and the URL/users/123
is accessed, React Router would extract the value123
from the URL and pass it to the component as a prop with the nameid
. You can then use this prop to render the appropriate content for that user.Dynamic parameters allow you to create flexible and dynamic routes that can handle a variety of URLs. They are commonly used in applications where the content or data is generated dynamically based on user input or other factors.