Friday, May 9, 2025
  • Home
  • About Us
  • Disclaimer
  • Contact Us
  • Terms & Conditions
  • Privacy Policy
T3llam
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment
No Result
View All Result
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment
No Result
View All Result
T3llam
No Result
View All Result
Home Services & Software

ReactJs | Methods to create Magento 2 product web page utilizing ReactJS

admin by admin
September 25, 2024
in Services & Software
0
ReactJs | Methods to create Magento 2 product web page utilizing ReactJS
0
SHARES
0
VIEWS
Share on FacebookShare on Twitter


Discover ways to construct a Magento 2 product web page utilizing ReactJs in a headless growth atmosphere.

By leverage of headless structure, we’ll seamlessly combine Magento 2 backend with React, permitting for a extra versatile and dynamic person expertise and the complete potential of headless commerce.

Introduction

Making a product web page in React on your Magento 2 retailer can considerably affect whether or not a person decides to purchase your product or transfer on.

This information will take you thru the method of constructing an environment friendly and user-friendly product web page, that includes code examples and detailed explanations.

You’ll achieve invaluable insights into React.js growth alongside the way in which.

You’ll discover ways to fetch information from Magento 2 GraphQL API, handle state, and design a responsive format that enhances the general buying expertise.

Magento Product Page with ReactJs

Implementation

Now, let’s implement the product web page step-by-step.

Setting Up Your Product Web page with ReactJs

1. Create Your ReactJs Venture:

Open your terminal and run the next command to create a brand new React.Js Venture

npx create-react-app my-magento-store
cd my-magento-store

2. Navigate to root listing:

cd my-magento-store

3. Set up Needed Packages In Your ReactJs:

As, I created this with Tailwind, Eslint, React router dom, TypeScript so:

// typescript and eslint
npm set up -D typescript eslint

// tailwind css
npm set up -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

// react router
npm set up react-router-dom

4. Set Up GraphQL Queries:

As we’re utilizing Magento 2 GraphQl API. So lets, create a folder on your GraphQL queries. This retains your code organized.

mkdir src/graphql
contact src/graphql/queries.ts
//In queries.ts, you possibly can outline your GraphQL question for fetching product particulars:
// graphql/queries.ts

export const GET_PRODUCT_BY_SKU = (sku: string) => `
{
    merchandise(filter: {sku: {eq: "${sku}"}}) {
        gadgets {
            id
            title
            rating_summary
            review_count
            sku
            stock_status
            value {
                regularPrice {
                    quantity {
                        worth
                        foreign money
                    }
                }
            }
            description {
                html
            }
            media_gallery {
                url
            }
        }
    }
}
`;

5. Create Your Product Web page In ReactJs:

Contained in the src listing, in a file known as App.tsx add routing construction:

import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import ProductPage from '../parts/ProductPage'; // Your product web page element

perform App() {
  return (
    
      
        
        {/* Different routes */}
      
    
  );
}

export default App;

6. Create a product web page element

mkdir src/parts
contact src/parts/ProductPage.tsx

And, add the next code in product web page element:

import React, { useEffect, useState } from "react";
import { GET_PRODUCT_BY_SKU } from "../graphql/queries";
import { StarIcon } from "./StarIcon";
import { HeartIcon } from "./Coronary heart";
import { ChartBarIcon } from "./CompareIcon";
import { useParams } from "react-router-dom";
interface ProductType {
  value: any;
  media_gallery: {
    url: string;
  }[];
  title: string;
  stock_status: string;
  sku: string;
  id: string;
}
const ProductPage: React.FC = () => {
  const { sku="" } = useParams<{ sku: string }>();
  const [product, setProduct] = useState(null);
  const [loading, setLoading] = useState(true);
  const APP_URL = MAGENTO_ENDPOINT/graphql;
  useEffect(() => {
    const fetchProduct = async () => {
      strive {
        const res = await fetch("APP_URL", {
          methodology: "POST",
          headers: {
            "Content material-Kind": "software/json",
          },
          physique: JSON.stringify({
            question: GET_PRODUCT_BY_SKU(sku),
          }),
        });

        const information = await res.json();
        const fetchedProduct = information.information.merchandise.gadgets[0] || null;
        setProduct(fetchedProduct);
      } catch (error) {
        console.error(error);
      } lastly {
        setLoading(false);
      }
    };

    fetchProduct();
  }, [sku]);

  if (loading) {
    return 

Loading...

; } if (!product) { return

No product discovered.

; } const priceData = product.value.regularPrice.quantity; const foreign money = "USD"; const worth = priceData?.worth; const value = worth?.toLocaleString("en-US", { model: "foreign money", foreign money, }); const gadgets = new Array(5).fill(0); return (
{product.media_gallery && product.media_gallery.size > 0 && ( {product.name} )}

{gadgets.map((ranking) => ( ranking ? "text-yellow-500 fill-yellow-500" : "text-gray-200"} h-5 w-5 flex-shrink-0`} aria-hidden="true" /> ))}

{merchandise.review_count} Critiques

Add Your Overview

{value}

{product?.stock_status?.exchange("_", " ")}

Sku# : {product?.sku}


); }; export default ProductPage;
react.js product page desktop view

MObile View

react.js product page mobile view

7. Run Your Utility:

Lastly, run your ReactJs app to see your product web page in motion:

npm begin

Conclusion

By following these steps, you’ve efficiently created a product web page utilizing React.Js that integrates seamlessly along with your Magento 2 backend.

This basis not solely shows important product data but additionally units the stage for additional enhancements.

You’ll be able to elevate the person expertise by including options similar to a buyer cart, person critiques, associated merchandise, and superior styling choices.

These additions is not going to solely enrich the performance but additionally create a extra partaking buying expertise on your clients.

Be happy to customise and increase upon this basis as wanted, tailoring it to satisfy your particular enterprise objectives and person wants. The chances are countless!

author-thumb

RelatedPosts

Person Information for WooCommerce WhatsApp Order Notifications

Person Information for WooCommerce WhatsApp Order Notifications

April 2, 2025
Report reveals overinflated opinion of infrastructure automation excellence

Report reveals overinflated opinion of infrastructure automation excellence

April 2, 2025
I have been kidnapped by Robert Caro

I have been kidnapped by Robert Caro

April 2, 2025
Previous Post

It is time for Blizzard to vary World of Warcraft’s enlargement formulation

Next Post

Apple TV+ to Stream ‘Peanuts’ Vacation Specials for Free Once more This Yr

Next Post
Apple TV+ to Stream ‘Peanuts’ Vacation Specials for Free Once more This Yr

Apple TV+ to Stream 'Peanuts' Vacation Specials for Free Once more This Yr

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • App (3,061)
  • Computing (4,342)
  • Gaming (9,491)
  • Home entertainment (633)
  • IOS (9,408)
  • Mobile (11,737)
  • Services & Software (3,935)
  • Tech (5,253)
  • Uncategorized (4)

Recent Posts

  • Essential Launch Intel You Must Know!
  • New Plex Cellular App With Streamlined Interface Rolling Out to Customers
  • I’ve had it with the present GPU market – and the costs for AMD Radeon companion playing cards on Finest Purchase are why
  • MCP: The brand new “USB-C for AI” that’s bringing fierce rivals collectively
  • Realme GT7’s processor confirmed, launching this month
  • App
  • Computing
  • Gaming
  • Home entertainment
  • IOS
  • Mobile
  • Services & Software
  • Tech
  • Uncategorized
  • Home
  • About Us
  • Disclaimer
  • Contact Us
  • Terms & Conditions
  • Privacy Policy

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies. However you may visit Cookie Settings to provide a controlled consent.
Cookie settingsACCEPT
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analyticsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functionalThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessaryThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-othersThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performanceThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policyThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Save & Accept