Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

cartservice

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cartservice

This React hook provides functionalities to manage cart operations such as adding, removing, and updating items in a shopping cart. It also calculates the total cart value and allows clearing the cart.

latest
Source
npmnpm
Version
1.1.3
Version published
Maintainers
1
Created
Source

CartService Hook with React

This React hook provides functionalities to manage cart operations such as adding, removing, and updating items in a shopping cart. It also calculates the total cart value and allows clearing the cart.

Usage

// Initialize cart from localStorage, or with an empty array if nothing is stored
const storedCart = localStorage.getItem("cartItems");
const initalValue = storedCart ? JSON.parse(storedCart) : [];
const { cartLength, cartItems, addToCart, removeFromCart, reduceFromCart, increaseToCart, cartTotal, clearCart } = useCartService(initalValue);

// Updates the localstorage on change of cartItems
useEffect(() => {
  localStorage.setItem("cartItems", JSON.stringify(cartItems));
}, [cartItems]);

Hook Functions

  • addToCart: Adds an item to the cart or increments the quantity if the item already exists.
  • removeFromCart: Removes an item from the cart based on the item id.
  • increaseToCart: Increases the quantity of an item in the cart.
  • reduceFromCart: Reduces the quantity of an item in the cart. The quantity doesn't go below 0.
  • cartTotal: Calculates the total cost of the items in the cart.
  • clearCart: Clears all the items from the cart.

Keywords

cartservice

FAQs

Package last updated on 21 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts