Create an E-commerce Products Page in ReactJS (2024)

Ben Woodman

Posted on

Create an E-commerce Products Page in ReactJS (2) Create an E-commerce Products Page in ReactJS (3) Create an E-commerce Products Page in ReactJS (4) Create an E-commerce Products Page in ReactJS (5) Create an E-commerce Products Page in ReactJS (6)

#react #tutorial #javascript

E-commerce has revolutionized every aspect of purchasing goods, and the need for skilled web developers has never been greater. In this post, I will walk you step-by-step through the creation of a custom products page using ReactJS. You can read more about React here.

Fiddle: https://jsfiddle.net/x159wmgj/
Github: https://github.com/benwoodman/React-Product-Page

Let's start by creating our products page. Here's a boilerplate template that we can use.

products.html

<html><head> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.2/css/bulma.min.css"> <link rel="stylesheet" href="styles.css"></head><body> <div id='wrapper'> <h1 class='title'>Cars for Sale</h1> <div id='product-catalog'></div> </div> <script src="components.js" type="text/babel"></script> <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script> <script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script> <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js" integrity="sha512-bZS47S7sPOxkjU/4Bt0zrhEtWx0y0CRkhEp8IckzK+ltifIIE9EMIMTuT/mEzoIMewUINruDBIR/jJnbguonqQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> <script type="text/babel" src="main.js"></script></body></html>

In a nutshell

  • In the head of the document, we are loading some predefined styles from the Bulma CSS library and a file containing our own custom styles we will use to construct our page.
  • In the body, we will put all of our content in a div named "wrapper". Right now the wrapper only contains a static title element and a div that we will fill dynamically with our products.
  • We then load our necessary scripts: Babel, React, ReactDOM, Axios, our custom components file, and a main.js file that we will use to set up our page.

Let's start by designing a static product tile that we can use as the template for the generated product tiles. Create a file named "components.js" in your root directory.

React separates these common "templates" into something called components. These components can be reused an infinite number of times to construct elements on the page, making it very easy to generate dynamic web pages.

We will create a new React component like so:

components.js

class ProductTile extends React.Component {}

React components are predefined classes that we can extend to create our own components. This is called inheritance in Object Oriented Programming.

We now can go ahead and start creating our first component, ProductTile. This will be an individual product listing on the product page and will look something like this.

Create an E-commerce Products Page in ReactJS (7)

For the design, we will use a predefined element called a Bulma card. You can read up more about that here.

In our ProductTile class, we will create a function named "render" that React will use to render our component in the DOM.

components.js

class ProductTile extends React.Component { render() { return ( ... ) }}

Within our return statement, we will add JSX code (which looks very similar to HTML syntax) to render our tile. We can use JSX code in our components file because of these two lines of code in our products.html file.

products.html

<script src="components.js" type="text/babel"></script><script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

We use the Babel interpreter to compile the JSX code into Javascript code that the browser can understand.

Let's add the tile JSX information.

components.js

class ProductTile extends React.Component { render() { return ( <div class="card"> <div class="card-image"> <figure class="image is-4by3"> <img src='img.jpg' alt="Placeholder image"></img> </figure> </div> <div class="card-content"> <p class="title product-title">MKX</p> <div class="content"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec iaculis mauris. <br></br> </div> <a class="button is-primary" href="product.html" target="_blank"> <strong>More Details</strong> </a> </div> </div> ) }}

Let's add some styles... (you can do whatever you want here).

styles.css

:root { --main-background-color: #F9FBF2; --main-top-background-color: #FBFEF9; --main-accent-color: #FB8F67; --title-color: #1E2019;}.button.is-primary { background-color: var(--main-accent-color);}.button.is-primary:hover { background-color: var(--title-color);}.title { color: var(--main-accent-color); font-family: 'Inconsolata', monospace; font-size: 3em;}#inner-large-banner { height: 40%; width: 100%; background-image: url('img/forest.jpg'); background-size: cover; background-position: 50% 25%;}#wrapper { width: 80%; min-height: 70%; /* margin-top: -100px; */ background-color: var(--main-top-background-color); margin: -100px auto 0 auto; padding: 30px; box-shadow: 0 0.5em 1em -0.125em rgb(10 10 10 / 10%), 0 0 0 1px rgb(10 10 10 / 2%); border-radius: .25rem;}#product-catalog { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; gap: 20px;}.product-title { font-size: 2em;}

We have now defined a static React component that we can render on our page. To test it, we will add a single line of code in our main.js file to render it within the "product-catalog" div in our products.html file.

main.js

ReactDOM.render(<ProductTile></ProductTile>, document.getElementById('product-catalog'));

Upon adding this line of code, you should see something like this appear on your page:

Create an E-commerce Products Page in ReactJS (8)

Incredible! Now let's set up a ProductCatalog component that will house multiple of these tiles.

components.js

class ProductCatalog extends React.Component { renderTile = () => { } render() { }}

I have set up two methods within our class: one to render an individual ProductTile and one to render the collection of ProductTiles. Let's start with our method to return an individual ProductTile.

components.js

class ProductCatalog extends React.Component { renderTile = () => { return <ProductTile></ProductTile>; } render() { return this.renderTile(); }}

We now have a ProductCatalog that returns a single ProductTile, but we need it to render more than one ProductTile. We can achieve this by creating a list, adding multiple ProductTiles to it, and then returning the list.

components.js

class ProductCatalog extends React.Component { renderTile = () => { return <ProductTile></ProductTile>; } render() { let tiles = []; for (let i = 0; i < 8; i++) { tiles.push(this.renderTile()); } return tiles; }}

Let's edit our main.js file to return a ProductCatalog rather than a ProductTile.

main.js

ReactDOM.render(<ProductCatalog></ProductCatalog>, document.getElementById('product-catalog'));

The result:

Create an E-commerce Products Page in ReactJS (9)

Our design is now complete! Now you're probably wondering how we customize each ProductTile to represent a separate product in the store. We need to start by fetching our JSON products file I talked about earlier. You can serve this up on a webserver or add it directly into your project.

main.js

// Our data:const products = [ { "id": "1", "name": "Touareg", "image": "/product_img/Mazda_CX-9_2011_34_20_270_37_6_76_68_200_16_AWD_7_4_SUV_gJa.jpg", "cost": "677.32", "short_description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec iaculis mauris.", "bullet_description": [ "Capacity Spacious: The 2 person tent designed for a roomy and convenient experience. The camping tent with an interior center height of 47 inches and 82.7 x 82.7 inches base dimensions, it is a spacious camp tent with plenty of space for you", "Stable & Safe: To keep your camp equipment dry, the sturdy polyester material on the outdoor tent body and the removable rain fly help to ensure water does not make its way into the inside of your waterproof tent for a dry travel", "Ventilation Design: Large window design for enhanced airflow. A D-style door will keep an open view. Also, two person tent comes with a storage pocket that helps to clear clutter and keep the inside of the dome tent organized. Great tents for camping", "Easy Set-Up & Carry: The camp tent equip with 2 ropes and 6 stakes for safe and stable. It only takes 1-2 people 5 minutes to set up. The carry bag weighs only 5.7lbs. A lightweight tent can be store in the car without taking up much space" ] }, { "id": "2", "name": "DeVille", "image": "/product_img/Chevrolet_Camaro_2019_31_18_270_20_4_74_52_188_20_RWD_4_2_Convertible_Xcu.jpg", "cost": "3474.41", "short_description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec iaculis mauris.", "bullet_description": [ "Capacity Spacious: The 2 person tent designed for a roomy and convenient experience. The camping tent with an interior center height of 47 inches and 82.7 x 82.7 inches base dimensions, it is a spacious camp tent with plenty of space for you", "Stable & Safe: To keep your camp equipment dry, the sturdy polyester material on the outdoor tent body and the removable rain fly help to ensure water does not make its way into the inside of your waterproof tent for a dry travel", "Ventilation Design: Large window design for enhanced airflow. A D-style door will keep an open view. Also, two person tent comes with a storage pocket that helps to clear clutter and keep the inside of the dome tent organized. Great tents for camping", "Easy Set-Up & Carry: The camp tent equip with 2 ropes and 6 stakes for safe and stable. It only takes 1-2 people 5 minutes to set up. The carry bag weighs only 5.7lbs. A lightweight tent can be store in the car without taking up much space" ] }, { "id": "3", "name": "F250", "image": "/product_img/Jaguar_F-Pace_2017_41_18_180_20_4_76_65_186_26_AWD_5_4_SUV_Thp.jpg", "cost": "2878.07", "short_description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec iaculis mauris.", "bullet_description": [ "Capacity Spacious: The 2 person tent designed for a roomy and convenient experience. The camping tent with an interior center height of 47 inches and 82.7 x 82.7 inches base dimensions, it is a spacious camp tent with plenty of space for you", "Stable & Safe: To keep your camp equipment dry, the sturdy polyester material on the outdoor tent body and the removable rain fly help to ensure water does not make its way into the inside of your waterproof tent for a dry travel", "Ventilation Design: Large window design for enhanced airflow. A D-style door will keep an open view. Also, two person tent comes with a storage pocket that helps to clear clutter and keep the inside of the dome tent organized. Great tents for camping", "Easy Set-Up & Carry: The camp tent equip with 2 ropes and 6 stakes for safe and stable. It only takes 1-2 people 5 minutes to set up. The carry bag weighs only 5.7lbs. A lightweight tent can be store in the car without taking up much space" ] }, { "id": "4", "name": "Corvette", "image": "/product_img/Bentley_Flying Spur_2011_181_19_550_60_12_77_55_208_11_AWD_5_4_4dr_FLx.jpg", "cost": "4379.01", "short_description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec iaculis mauris.", "bullet_description": [ "Capacity Spacious: The 2 person tent designed for a roomy and convenient experience. The camping tent with an interior center height of 47 inches and 82.7 x 82.7 inches base dimensions, it is a spacious camp tent with plenty of space for you", "Stable & Safe: To keep your camp equipment dry, the sturdy polyester material on the outdoor tent body and the removable rain fly help to ensure water does not make its way into the inside of your waterproof tent for a dry travel", "Ventilation Design: Large window design for enhanced airflow. A D-style door will keep an open view. Also, two person tent comes with a storage pocket that helps to clear clutter and keep the inside of the dome tent organized. Great tents for camping", "Easy Set-Up & Carry: The camp tent equip with 2 ropes and 6 stakes for safe and stable. It only takes 1-2 people 5 minutes to set up. The carry bag weighs only 5.7lbs. A lightweight tent can be store in the car without taking up much space" ] }, { "id": "5", "name": "MKX", "image": "/product_img/Audi_R8_2017_189_19_610_52_10_76_48_174_14_AWD_2_2_2dr_nUS.jpg", "cost": "3274.66", "short_description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec iaculis mauris.", "bullet_description": [ "Capacity Spacious: The 2 person tent designed for a roomy and convenient experience. The camping tent with an interior center height of 47 inches and 82.7 x 82.7 inches base dimensions, it is a spacious camp tent with plenty of space for you", "Stable & Safe: To keep your camp equipment dry, the sturdy polyester material on the outdoor tent body and the removable rain fly help to ensure water does not make its way into the inside of your waterproof tent for a dry travel", "Ventilation Design: Large window design for enhanced airflow. A D-style door will keep an open view. Also, two person tent comes with a storage pocket that helps to clear clutter and keep the inside of the dome tent organized. Great tents for camping", "Easy Set-Up & Carry: The camp tent equip with 2 ropes and 6 stakes for safe and stable. It only takes 1-2 people 5 minutes to set up. The carry bag weighs only 5.7lbs. A lightweight tent can be store in the car without taking up much space" ] }, { "id": "6", "name": "Accord", "image": "/product_img/Cadillac_CT6_2016_58_18_260_20_4_74_57_204_22_RWD_5_4_4dr_yfN.jpg", "cost": "758.8", "short_description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec iaculis mauris.", "bullet_description": [ "Capacity Spacious: The 2 person tent designed for a roomy and convenient experience. The camping tent with an interior center height of 47 inches and 82.7 x 82.7 inches base dimensions, it is a spacious camp tent with plenty of space for you", "Stable & Safe: To keep your camp equipment dry, the sturdy polyester material on the outdoor tent body and the removable rain fly help to ensure water does not make its way into the inside of your waterproof tent for a dry travel", "Ventilation Design: Large window design for enhanced airflow. A D-style door will keep an open view. Also, two person tent comes with a storage pocket that helps to clear clutter and keep the inside of the dome tent organized. Great tents for camping", "Easy Set-Up & Carry: The camp tent equip with 2 ropes and 6 stakes for safe and stable. It only takes 1-2 people 5 minutes to set up. The carry bag weighs only 5.7lbs. A lightweight tent can be store in the car without taking up much space" ] }, { "id": "7", "name": "Suburban 2500", "image": "/product_img/Chevrolet_Silverado 2500HD_2017_54_20_360_60_8_80_78_nan_nan_RWD_5_4_Pickup_FvP.jpg", "cost": "2691.62", "short_description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec iaculis mauris.", "bullet_description": [ "Capacity Spacious: The 2 person tent designed for a roomy and convenient experience. The camping tent with an interior center height of 47 inches and 82.7 x 82.7 inches base dimensions, it is a spacious camp tent with plenty of space for you", "Stable & Safe: To keep your camp equipment dry, the sturdy polyester material on the outdoor tent body and the removable rain fly help to ensure water does not make its way into the inside of your waterproof tent for a dry travel", "Ventilation Design: Large window design for enhanced airflow. A D-style door will keep an open view. Also, two person tent comes with a storage pocket that helps to clear clutter and keep the inside of the dome tent organized. Great tents for camping", "Easy Set-Up & Carry: The camp tent equip with 2 ropes and 6 stakes for safe and stable. It only takes 1-2 people 5 minutes to set up. The carry bag weighs only 5.7lbs. A lightweight tent can be store in the car without taking up much space" ] }, { "id": "8", "name": "LR2", "image": "/product_img/Porsche_Macan_2015_49_19_340_30_6_76_63_184_17_AWD_5_4_SUV_mjd.jpg", "cost": "4801.29", "short_description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec iaculis mauris.", "bullet_description": [ "Capacity Spacious: The 2 person tent designed for a roomy and convenient experience. The camping tent with an interior center height of 47 inches and 82.7 x 82.7 inches base dimensions, it is a spacious camp tent with plenty of space for you", "Stable & Safe: To keep your camp equipment dry, the sturdy polyester material on the outdoor tent body and the removable rain fly help to ensure water does not make its way into the inside of your waterproof tent for a dry travel", "Ventilation Design: Large window design for enhanced airflow. A D-style door will keep an open view. Also, two person tent comes with a storage pocket that helps to clear clutter and keep the inside of the dome tent organized. Great tents for camping", "Easy Set-Up & Carry: The camp tent equip with 2 ropes and 6 stakes for safe and stable. It only takes 1-2 people 5 minutes to set up. The carry bag weighs only 5.7lbs. A lightweight tent can be store in the car without taking up much space" ] }]...

We can pass this list of products to the ProductCatalog component by using something called component props. The syntax is very similar to HTML properties. We pass it like so:

main.js

...const setup = function() { ReactDOM.render(<ProductCatalog products={products}></ProductCatalog>, document.getElementById('product-catalog'));}setup();

Now, when ProductCatalog is rendered by React, it will have access to the products JSON list. Let's edit our ProductCatalog element to render ProductTiles with this information.

components.js

class ProductCatalog extends React.Component { renderTile = (current_item) => { return <ProductTile product={current_item}></ProductTile>; } render() { let tiles = []; for (let i = 0; i < this.props.products.length; i++) { const current_item = this.props.products[i]; tiles.push(this.renderTile(current_item)); } return tiles; }}...

Notice that we can access our list of products by the "this.props.products" property. Notice how we also pass the current product to the renderTile method so that we can pass the individual product's information to the ProductTile component. The last step is to edit our ProductTile component to use the product information passed to it in the last step.

components.js

...class ProductTile extends React.Component { render() { return ( <div class="card"> <div class="card-image"> <figure class="image is-4by3"> <img src={this.props.product.image} alt="Placeholder image"></img> </figure> </div> <div class="card-content"> <p class="title product-title">{this.props.product.name}</p> <div class="content"> {this.props.product.short_description} <br></br> </div> <a class="button is-primary" href={"product.html?id=" + this.props.product.id.toString()} target="_blank"> <strong>More Details</strong> </a> </div> </div> ) }}

We can insert elements from our product into the JSX code by using curly braces surrounding the value. So for example, to print the product name we use: <p class="title product-title">{this.props.product.name}</p>

Upon loading our page, we should be greeted with something like the following:

Create an E-commerce Products Page in ReactJS (10)

Your product catalog is now finished! To recap, you created a ProductTile that houses an individual product and a ProductCatalog that houses a collection of ProductTiles and rendered them on the DOM. Congratulations!

Create an E-commerce Products Page in ReactJS (2024)

FAQs

How to create e-commerce website using reactjs? ›

How to Build a React E-Commerce Web App [Live Demo & Tutorial]
  1. Creating a new app with Next. js.
  2. Adding products to the React app.
  3. Integrating a shopping cart to our web app.
  4. Defining products within Snipcart.
  5. Setting up a cart summary and cart preview.
Mar 21, 2022

How do I create a product page in React JS? ›

Overview
  1. Create a new React app and install the Commerce. js SDK.
  2. Create React components to display static product data.
  3. Hook up the components to the Chec API using Commerce. js to display live product data.

Is React JS good for eCommerce? ›

JavaScript and React are among the most popular stacks for building ecommerce applications. React is a JavaScript library for designing user interfaces. It enables developers to produce reusable, modular components that are simple to integrate into bigger applications.

How do you code an eCommerce website from scratch? ›

A 7-step recipe for building an eCommerce website from scratch
  1. Pick the right eCommerce platform.
  2. Select a theme and start customizing.
  3. Create your product pages.
  4. Set up payments.
  5. Set up shipping and delivery.
  6. Connect your domain.
  7. Test and launch.
Dec 31, 2022

Can I build my own e-commerce website? ›

Yes, you can easily build an e-commerce website on your own. You need to follow these steps: Buy a custom domain name for your e-commerce website. Opt for an SaaS e-commerce solution.

How can I add e-commerce to my website? ›

How to Add eCommerce to Your Website?
  1. Integrate eCommerce Into Your Site Using Plugins. ...
  2. Tag on a Separate Standalone eCommerce to the Existing Site. ...
  3. Add a Shopping Cart or a Payment Gateway. ...
  4. Connect to Marketplaces. ...
  5. Add a Shopify Button to Your Website.
Jan 19, 2022

How do I create a new product page? ›

15 tips on how to design a product page
  1. Write a compelling product description. ...
  2. Be direct. ...
  3. Answer questions that shoppers might have about your product. ...
  4. Bring your product to life with images. ...
  5. Multiple images are better than just one. ...
  6. Show different angles. ...
  7. Use lifestyle images. ...
  8. Give shoppers a clear call to action.

How do I create a custom product page? ›

Key steps to set up and configure custom product pages
  1. Step 1 — Choose your app. ...
  2. Step 2 — Develop a custom product page. ...
  3. Step 3 — Start from scratch or edit an existing page. ...
  4. Step 4 — Add visual elements. ...
  5. Step 5 — Submit for review. ...
  6. Step 6 — Edit. ...
  7. Step 7 — Add the CPP to your ad variations.
Dec 27, 2022

Is React enough to make a website? ›

For example, you can use React whether you're building a website, web app, mobile app, or virtual reality applications. It can be tough to make sweeping changes to an existing website. With React, you can start by changing a few elements before eventually transitioning the entire site to React.

Why ReactJS is better than WordPress? ›

ReactJS improves performance due to virtual DOM. The DOM is a multi-platform programming API that deals with HTML, XML or XHTML. Most developers face a problem when the DOM is updated, which slows down the performance of applications. ReactJS solved this problem by introducing virtual DOM.

How much is a React eCommerce website? ›

Average Cost of Building a React Website

However, you can expect the Reactjs website cost to be anywhere from $5,000 to $50,000, depending on the complexity of the project. A simple brochure website may be on the lower end of the spectrum, while a complex e-commerce platform can cost upwards of $50,000.

Is it hard to code an eCommerce website? ›

Starting an e-commerce business is a big endeavor, but it's less complicated than you might expect. Most e-commerce platforms are designed for beginners, even those with no coding experience. An e-commerce system with CMS capabilities makes it possible to build your online store in a matter of days or even hours.

How do I create an eCommerce website step by step? ›

How to Build an Ecommerce Website Step-by-Step
  1. Select your perfect ecommerce platform.
  2. Purchase a domain name.
  3. Find a developer.
  4. Pick your ecommerce theme.
  5. Customize your ecommerce template.
  6. Add your products.
  7. Set up payment options.
  8. Sort out your shipping settings.

Can we make a eCommerce website without coding? ›

Most of the builders offer integrated solutions like payment gateways, inventory management and shipping, so you can construct a professional-looking website without the headache of coding. Squarespace and Wix even offer built-in SEO tools and website analytics, so you can keep track of your bottom line.

Can I build a website with react JS? ›

React is a fantastic and wildly popular tool for building websites and apps. It creates a world where JavaScript and HTML live in happy harmony in the same files, and it efficiently renders your ever-changing data to the browser.

Can we create website using ReactJS? ›

ReactJS is a JavaScript framework that makes it easy to design and develop interactive user interfaces. It is popular among developers worldwide due to its efficiency, flexibility, and ease of use in developing web and mobile applications, including react web app development.

How much is a react ecommerce website? ›

Average Cost of Building a React Website

However, you can expect the Reactjs website cost to be anywhere from $5,000 to $50,000, depending on the complexity of the project. A simple brochure website may be on the lower end of the spectrum, while a complex e-commerce platform can cost upwards of $50,000.

How do I create a blog website with ReactJS? ›

In this article, we have created the blog app using react js, First of all, we have created the project name blog by entering the command npx create-react-app blog and installing all modules. Then we create the folder name component under src and make two jsx file post. jsx and posts.

References

Top Articles
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 5877

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.