Home > Web Front-end > JS Tutorial > How to use React to develop a simple and easy-to-use online shopping website

How to use React to develop a simple and easy-to-use online shopping website

PHPz
Release: 2023-09-27 08:36:11
Original
1017 people have browsed it

How to use React to develop a simple and easy-to-use online shopping website

How to develop a simple and easy-to-use online shopping website with React

Overview:
React is a JavaScript library for building user interfaces that can Helps us develop complex web applications in a composable manner. In this article, we will learn how to develop a simple and easy-to-use online shopping website using React. We will use core concepts such as React components and state management to implement this website.

Environment preparation:
First, make sure Node.js and NPM package manager are installed on your computer. Then, install the create-react-app required to create a React app with the following command:

npx create-react-app shopping-site
Copy after login

Create React Component:
Create a new component named Product which will represent Products on our website. Create a new file Product.js in the src directory and add the following code:

import React from 'react';

class Product extends React.Component {
    render() {
        return (
            <div>
                <h2>{this.props.name}</h2>
                <p>{this.props.description}</p>
                <p>${this.props.price}</p>
                <button>Add to Cart</button>
            </div>
        );
    }
}

export default Product;
Copy after login

This component will accept name, description and price as attributes and display them on the page. There is also an "Add to Cart" button.

Create product data:
Create a file named products.js in the src directory and add the following code:

const products = [
    {
        id: 1,
        name: 'Product 1',
        description: 'This is product 1',
        price: 10
    },
    {
        id: 2,
        name: 'Product 2',
        description: 'This is product 2',
        price: 20
    },
    {
        id: 3,
        name: 'Product 3',
        description: 'This is product 3',
        price: 30
    }
];

export default products;
Copy after login

Create the shopping cart component:
Create a new component called Cart which will represent the user's shopping cart. Create a new file Cart.js in the src directory and add the following code:

import React from 'react';
import Product from './Product';
import products from './products';

class Cart extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            cartItems: []
        };
    }

    addToCart(product) {
        this.setState(prevState => ({
            cartItems: [...prevState.cartItems, product]
        }));
    }

    render() {
        return (
            <div>
                <h1>Shopping Cart</h1>
                {products.map(product => (
                    <Product
                        key={product.id}
                        name={product.name}
                        description={product.description}
                        price={product.price}
                        addToCart={() => this.addToCart(product)}
                    />
                ))}
                <h2>Cart Items</h2>
                {this.state.cartItems.map(item => (
                    <div key={item.id}>
                        <p>{item.name}</p>
                        <p>${item.price}</p>
                    </div>
                ))}
            </div>
        );
    }
}

export default Cart;
Copy after login

Note that we used the Product component in the Cart component and passed Pass the addToCart method as an attribute to the Product component to implement the function of adding products to the shopping cart when the user clicks the "Add to Cart" button.

Rendering application:
Now, in the index.js file in the src directory, the Cart component will be rendered, replacing the default rendering code:

import React from 'react';
import ReactDOM from 'react-dom';
import Cart from './Cart';

ReactDOM.render(
    <React.StrictMode>
        <Cart />
    </React.StrictMode>,
    document.getElementById('root')
);
Copy after login

In this example, we use Use React's ReactDOM.render() method to render the Cart component to the root element on the page.

Run the application:
Start the development server with the following command:

npm start
Copy after login

Now, you can visit http://localhost:3000 in the browser to see your online Shopping website! When you click the "Add to Cart" button for an item, the item will be added to the shopping cart.

Conclusion:
By using React, we can easily build a simple and easy-to-use online shopping website. Using React's componentization and state management capabilities, we can efficiently build and maintain complex web applications. The above is a simple example that you can extend and customize to meet the needs of real projects. Hope this article helps you!

The above is the detailed content of How to use React to develop a simple and easy-to-use online shopping website. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template