npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-ecommerce-kit

v1.1.2

Published

A simple shopping cart provider for React applications.

Downloads

26

Readme

use-ecommerce-kit

use-ecommerce-kit is a React library designed for ecommerce state management. It enables users to perform actions like adding items to their shopping cart, adjusting quantities, removing items, clearing the entire cart, and viewing its contents.

Installation

To install the library using npm, run the following command:

npm install use-ecommerce-kit

Note: This library heavily relies on @tanstack/react-query.

Usage

The following parameters are required in order for the items to load properly in the cart

type ProductTypes = {
    productId: string,
    name: string,
    price: number,
    amount: number[],
    // generic type for any other fields
    [key: string]: any,
};

You can also use any other fields That fit within your ecommerce requirements.

Cart Provider

Each hook must be used within the CartProvider JSX element, which is what provides the cart state with the context.

import React from "react";
import { CartProvider } from "use-ecommerce-kit";

const App = () => {
    return (
        <CartProvider>
            <ChildComponents />
        </CartProvider>
    );
};

Add to cart

import React from "react";
import { useCartContext } from "use-ecommerce-kit";

const ShopItem = () => {
    const productInfo = {
        productId: "1234",
        name: "T-Shirt",
        price: 23,
        // optional fields
        image: "src/asset.png",
        description: "fresh drip",
    };
    const { addToCart } = useCartContext();

    const handleCart = () => {
        //  passes the ID, amount added, and product info to context
        addToCart(productInfo.productId, 1, productInfo);
    };

    return (
        <div>
            <h1>{productInfo.name}</h1>
            <img src={productInfo.image} alt="product-image" />
            <h4>${contentEntry.fields.price.toFixed(2)}</h4>
            <p>{productInfo.decription}</p>
            <button onClick={handleCart}>Add to Cart</button>
        </div>
    );
};

Increase and Decrease Quantities

import React from "react";
import { useCartContext } from "use-ecommerce-kit";

const CartItem = () => {
    const { state, increase, decrease, remove } = useCartContext();

    return (
        <div>
        {state.cart.map((item, index) =>
            <div key={index}>
                <h3>{item.name}</h3>
                <p>Quantity: {item.amount.length}</p>
                <button onClick={() => increase(item.productId)}>Increase</button>
                <button onClick={() => decrease(item.productId)}>Decrease</button>
                <button onClick={() => remove(item.productId)}>Remove</button>
            </div>
        )}
         </div>
    );
};

Clearing the cart

import React from "react";
import { useCartContext } from "use-ecommerce-kit";

const CartSummary = () => {
    const { clear } = useCartContext();

    return (
        <div>
            <h2>Cart Summary</h2>
            <button onClick={clear}>Clear Cart</button>
        </div>
    );
};

Display total price & quantity

import React from "react";
import { useCartContext } from "use-ecommerce-kit";

const CartTotal = () => {
    const { state } = useCartContext();

    return (
        <div>
            // total price
            <h2>Total Price: ${state.total.toFixed(2)}</h2>
            // total quantity 
            <h1>{state.amount}</h1>
        </div>
    );
};