Add Routing and Make Cart Page
Hello viewers, i am back again for you and now we will start from redux-saga. So, we are ready for implemenation of react routing in redux saga and set result. Actually we are making some project like E-commerce where we can implement searchbar, product add to cart, remove from cart, empty cart, cart section after adding some product and product section and a lot of things, So, don't worry i'm not going to leave you alone.
React
Certainly, Reactjs is a JavaScript library developed by Facenook for building user interfaces. It allows developers to create reusable UI components and effciently manage the state of their applications. Here are some key aspects of Reactjs.
Component-Based Architecture: ReactJS follows a component-based architecture, where the user interface is divided into small, reusable components. Components encapsulate their own logic, state, and rendering, making it easier to build and maintain complex user interfaces.
Virtual DOM: ReactJS uses a virtual representation of the DOM (Document Object Model), known as the Virtual DOM. When the state of a component changes, React updates the Virtual DOM.
JSX: JSX is a syntax extension for JavaScript used in React. It allows developers to write HTML-like code within JavaScript, making it easier to describe the structure and appearance of components. JSX code is transpiled to regular JavaScript using tools like Babel before being executed in the browser.
Hooks: React introduced Hooks in version 16.8 as a way to use state and other React features in functional components. Hooks allow developers to write reusable logic and manage state within functional components without the need for class components. The most commonly used hooks are useState for managing state and useEffect for handling side effects such as fetching data or subscribing to events.
React Router: React Router is a popular routing library for React applications. It enables developers to create single-page applications with multiple views and handles routing between different components based on the URL.
State Management: React provides a flexible ecosystem of state management solutions. While React's built-in state management (useState ) is suitable for managing local component state, more complex applications may benefit from additional state management libraries like Redux. These libraries help manage global application state and provide predictable ways to update and access the state.
ReactJS has gained widespread popularity due to its performance, reusability, and declarative approach to building user interfaces. It has large community.
Topic we will cover -
# Install React router dom
# Make Main and cart page
#
Add Link with pages
# Add Style for Header and Logo
# Interview Question
Don't Forget to Check - https://www.kumaratuljaiswal.in/2023/09/get-data-in-component-from-redux.html
Don't Forget to Check - https://www.kumaratuljaiswal.in/2023/11/remove-from-cart-in-react-redux.html
Don't Forget to Check - https://www.kumaratuljaiswal.in/2023/11/configure-saga-middleware-in-react.html
Don't Forget to Check - https://www.kumaratuljaiswal.in/2023/11/remove-to-cart-with-id-react-redux-saga.html
React-Router-Dom Installation
We will install react-router-dom (https://reactrouter.com) by this command (npm i react-router-dom or
npm install react-router-dom) after installing we have a wrapper which is used to wrapping the
entire website for routing becuase when we want route in any page of website
we can easily redirect the page in any one of them website without Loading the
entire website.
Now Wrapping time... we will use wrapping in Main.js or
Index.js file. Here we import BrowserRouter form
react-router-dom
import { BrowserRouter } from 'react-router-dom' <BrowserRouter> <App /> </BrowserRouter>
Whole Code
import React from 'react' import ReactDOM from 'react-dom/client' import App from './App.jsx' import './index.css' import { Provider } from 'react-redux' import Store from './redux/Store.jsx' import { BrowserRouter } from 'react-router-dom' //www.kumaratuljaiswal.in www.hackingtruth.in console.log(Store) ReactDOM.createRoot(document.getElementById('root')).render( <Provider store={Store}> <BrowserRouter> <App /> </BrowserRouter> </Provider> )
Now we are ready to implement routing in our code. Here we import
Routes and Route from react-router-dom and using route
tag we define a path where we want redirect the page and in element we
define the component.
import { Routes, Route } from "react-router-dom"; <Routes> <Route path="/" element={<MainHeader />} /> <Route path="/cart" element={<Cart />} /> </Routes>
Simply make a page (Cart.js) component and put it into with routes for the page redirecting.
1. Import the Routes and Route and useNavigate from react-router-dom.
2. make a navigate with const.
3. simple create a text for redirecting back to Home Page.
import React from "react"; import { Routes, Route, useNavigate } from "react-router-dom"; function Cart() { const navigate = useNavigate() return ( <div> <div className="text-center mx-auto max-w-[1240px] "> <h1 className="text-blue uppercase underline font-bold first-letter:text-[red] pb-4 pt-4 cursor-pointer" onClick={() => navigate('/')}>Product List</h1> <h1>Cart Page</h1> </div> </div> ); } export default Cart;
Now Cart.jsx component put with route in App.jsx.
import React from "react"; import Header from "./components/Header"; import MainHeader from "./components/MainHeader"; import { Routes, Route } from "react-router-dom"; import Cart from "./components/Cart"; function App() { return ( <div> <> <Header /> {/* <MainHeader /> */} <Routes> <Route path="/" element={<MainHeader />} /> <Route path="/cart" element={<Cart />} /> </Routes> </> </div> ); } export default App;
# Install redux and saga packages - CLICK HERE
# Make reducer wrapper - CLICK HERE
# Action in reducer - CLICK HERE
# Reducer in redux -
CLICK HERE
# Switch Stmt in redux -
CLICK HERE
# Get data in component from redux - CLICK HERE
# Remove from cart - CLICK HERE
# Add Redux Toolkit in react redux saga - CLICK HERE
# Configure MiddleWare saga - CLICK HERE
# Call API with Saga and Set Result in react redux saga - CLICK HERE
# Product list ui with API data in react redux saga - CLICK HERE
# Remove to Cart with ID react redux saga -
CLICK HERE
Disclaimer