Call API with Saga and Set Result
Hello viewers, i am back again for you and now we will start from redux-saga. So, we are ready for implemenation of Call API with 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 -
# Call Saga function from Action
# Make function in Saga for API
#
Send Result from Saga to Action
# Pass data to Reducer
# Get data in Main Component
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
Call Saga function from Action
We have already discussed about generator function in javascript (
CLICK HERE FOR MORE INFO
). In this file ( ProductSaga.jsx ) we will import takeEvery function from
"redux-saga/effects" with using yield .
ProductSaga.jsx
import { takeEvery } from 'redux-saga/effects'; import {PRODUCT_LIST} from './Constant'; function* getProducts(){ console.log("API CALL DONE") } function* ProductSaga() { yield takeEvery(PRODUCT_LIST, getProducts) } export default ProductSaga
ProductAction.jsx
import { PRODUCT_LIST } from "./Constant"; export const productList = () => { return { type: PRODUCT_LIST, data: 'hackingtruth.in' } }
Here when call ***productList*** action, there is generator type like from ProductAction.jsx file return type (type:PRODUCT_LIST) and then calling done from ProductSaga.jsx file with the help of productSaga() function and then inside this takeEvery function, call getProducts Function so after that overall lastly call getProducts() function and call API.
API Calling
function* getProducts(){ //console.log("API CALL DONE") let data = yield fetch(`http://localhost:3000/products`); data = yield data.json(); console.log("action is called", data) }
with this command (json-server product.json) in cmd prompt you can run json server.
As you can see there is six product listing by calling when we click on
Get Product List button.
Now we will get move from ProductAction TO ProductSaga TO ProductReducer File
ProductReducer.jsx
import {SET_PRODUCT_LIST } from './Constant'; export const productData = (data = [], action) => { switch(action.type) { case SET_PRODUCT_LIST: console.log("SET_PRODUCT_LIST CONDITION", action) return [ ...action.data]; default: return data; } } //www.kumaratuljaiswal.in #www.hackingtruth.in
Full Code (ProductSaga.jsx)
import { takeEvery, put } from 'redux-saga/effects'; import {ADD_TO_CART, PRODUCT_LIST, SET_PRODUCT_LIST} from './Constant'; function* getProducts(){ let data = yield fetch(`http://localhost:3000/products`); data = yield data.json(); yield put({ type: SET_PRODUCT_LIST, data}) } function* ProductSaga() { yield takeEvery(PRODUCT_LIST, getProducts) } export default ProductSaga
Full Code (ProductAction.jsx)
import { PRODUCT_LIST } from "./Constant"; export const productList = () => { return { type: PRODUCT_LIST, } }
Full Code (ProductReducer.jsx)
import { PRODUCT_LIST, SET_PRODUCT_LIST } from './Constant'; export const productData = (data = [], action) => { switch(action.type) { case SET_PRODUCT_LIST: console.log("SET_PRODUCT_LIST CONDITION", action) return [ ...action.data]; default: return data; } } //www.kumaratuljaiswal.in #www.hackingtruth.in
# 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
Disclaimer
0 comments:
Post a Comment
For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.