1.0.1 ā€¢ Published 2 years ago

react-use-wishlist v1.0.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

Why?

  • No dependencies
  • šŸ’³ Not tied to any payment gateway, or checkout - create your own!
  • šŸ”„ Persistent wishlists with local storage, or your own adapter
  • ā­ļø Supports multiples wishlists per page
  • šŸ›’ Flexible wishlist item schema
  • šŸ„ž Works with Next, Gatsby, React
  • ā™»ļø Trigger your own side effects with wishlist handlers (on item add, remove)
  • šŸ›  Built with TypeScript
  • āœ… Fully tested
  • šŸŒ® Used by Heathway Foods

Quick Start

import { WishlistProvider, useWishlist } from "react-use-wishlist";

function Page() {
  const { addWishlistItem } = useWishlist();

  const products = [
    {
      id: 1,
      name: "Malm",
      price: 9900
    },
    {
      id: 2,
      name: "Nordli",
      price: 16500,
    },
    {
      id: 3,
      name: "Kullen",
      price: 4500
    },
  ];

  return (
    <div>
      {products.map((p) => (
        <div key={p.id}>
          <button onClick={() => addWishlistItem(p)}>Add to wishlist</button>
        </div>
      ))}
    </div>
  );
}

function Wishlist() {
  const {
    isWishlistEmpty,
    totalWishlistItems,
    items,
    removeWishlistItem,
  } = useWishlist();

  if (isWishlistEmpty) return <p>Your wishlist is empty</p>;

  return (
    <>
      <h1>Wishlist ({totalWishlistItems})</h1>

      <ul>
        {items.map((item) => (
          <li key={item.id}>
            {item.name} &mdash;
            <button onClick={() => removeWishlistItem(item.id)}>&times;</button>
          </li>
        ))}
      </ul>
    </>
  );
}

function App() {
  return (
    <WishlistProvider>
      <Page />
      <Wishlist />
    </WishlistProvider>
  );
}

Install

npm install react-use-wishlist # yarn add react-use-wishlist

WishlistProvider

You will need to wrap your application with the WishlistProvider component so that the useWishlist hook can access the wishlist state.

Wishlists are persisted across visits using localStorage, unless you specify your own storage adapter.

Usage

import React from "react";
import ReactDOM from "react-dom";
import { WishlistProvider } from "react-use-wishlist";

ReactDOM.render(
  <WishlistProvider>{/* render app/wishlist here */}</WishlistProvider>,
  document.getElementById("root")
);

Props

PropRequiredDescription
idNoid for your wishlist to enable automatic wishlist retrieval via window.localStorage. If you pass a id then you can use multiple instances of WishlistProvider.
onSetItemsNoTriggered only when setWishlistItems invoked.
onItemAddNoTriggered on items added to your wishlist
onItemRemoveNoTriggered on items removed from your wishlist.
storageNoMust return [getter, setter].
metadataNoCustom global state on the wishlist. Stored inside of metadata.

useWishlist

The useWishlist hook exposes all the getter/setters for your wishlist state.

setWishlistItems(items)

The setWishlistItems method should be used to set all items in the wishlist. This will overwrite any existing wishlist items.

Args

  • items[] (Required): An array of wishlist item object. You must provide an id and price value for new items that you add to wishlist.

Usage

import { useWishlist } from "react-use-wishlist";

const { setWishlistItems } = useWishlist();

const products = [
  {
    id: "ckb64v21u000001ksgw2s42ku",
    name: "Fresh Foam 1080v9",
    brand: "New Balance",
    color: "Neon Emerald with Dark Neptune",
    size: "US 10",
    width: "B - Standard",
    sku: "W1080LN9",
    price: 15000,
  },
  {
    id: "cjld2cjxh0000qzrmn831i7rn",
    name: "Fresh Foam 1080v9",
    brand: "New Balance",
    color: "Neon Emerald with Dark Neptune",
    size: "US 9",
    width: "B - Standard",
    sku: "W1080LN9",
    price: 15000,
  },
];

setWishlistItems(products);

addWishlistItem(item)

The addWishlistItem method should be used to add items to the wishlist.

Args

  • item (Required): An object that represents your wishlist item. You must provide an id and price value for new items that you add to wishlist.

Usage

import { useWishlist } from "react-use-wishlist";

const { addWishlistItem } = useWishlist();

const product = {
  id: "cjld2cjxh0000qzrmn831i7rn",
  name: "Fresh Foam 1080v9",
  brand: "New Balance",
  color: "Neon Emerald with Dark Neptune",
  size: "US 9",
  width: "B - Standard",
  sku: "W1080LN9",
  price: 15000,
};

addWishlistItem(product);

removeWishlistItem(itemId)

The removeWishlistItem method should be used to remove an item from the wishlist.

Args

  • itemId (Required): The wishlist item id you want to remove.

Usage

import { useWishlist } from "react-use-wishlist";

const { removeWishlistItem } = useWishlist();

removeWishlistItem("cjld2cjxh0000qzrmn831i7rn");

emptyWishlist()

The emptyWishlist() method should be used to remove all wishlist items, and resetting wishlist totals to the default 0 values.

Usage

import { useWishlist } from "react-use-wishlist";

const { emptyWishlist } = useWishlist();

emptyWishlist();

clearWishlistMetadata()

The clearWishlistMetadata() will reset the metadata to an empty object.

Usage

import { useWishlist } from "react-use-wishlist";

const { clearWishlistMetadata } = useWishlist();

clearWishlistMetadata();

setWishlistMetadata(object)

The setWishlistMetadata() will replace the metadata object on the wishlist. You must pass it an object.

Args

  • object: A object with key/value pairs. The key being a string.

Usage

import { useWishlist } from "react-use-wishlist";

const { setWishlistMetadata } = useWishlist();

setWishlistMetadata({ notes: "This is the only metadata" });

updateWishlistMetadata(object)

The updateWishlistMetadata() will update the metadata object on the wishlist. You must pass it an object. This will merge the passed object with the existing metadata.

Args

  • object: A object with key/value pairs. The key being a string.

Usage

import { useWishlist } from "react-use-wishlist";

const { updateWishlistMetadata } = useWishlist();

updateWishlistMetadata({ notes: "Leave in shed" });

items = []

This will return the current wishlist items in an array.

Usage

import { useWishlist } from "react-use-wishlist";

const { items } = useWishlist();

isWishlistEmpty = false

A quick and easy way to check if the wishlist is empty. Returned as a boolean.

Usage

import { useWishlist } from "react-use-wishlist";

const { isWishlistEmpty } = useWishlist();

getWishlistItem(itemId)

Get a specific wishlist item by id. Returns the item object.

Args

  • itemId (Required): The id of the item you're fetching.

Usage

import { useWishlist } from "react-use-wishlist";

const { getWishlistItem } = useWishlist();

const myItem = getWishlistItem("cjld2cjxh0000qzrmn831i7rn");

inWishlist(itemId)

Quickly check if an item is in the wishlist. Returned as a boolean.

Args

  • itemId (Required): The id of the item you're looking for.

Usage

import { useWishlist } from "react-use-wishlist";

const { inWishlist } = useWishlist();

inWishlist("cjld2cjxh0000qzrmn831i7rn") ? "In wishlist" : "Not in wishlist";

totalWishlistItems = 0

This returns the items in the wishlist as an integer.

Usage

import { useWishlist } from "react-use-wishlist";

const { totalItems } = useWishlist();

wishlistTotal = 0

This returns the total value of all items in the wishlist.

Usage

import { useWishlist } from "react-use-wishlist";

const { wishlistTotal } = useWishlist();

metadata = {}

This returns the metadata set with updateWishlistMetadata. This is useful for storing additional wishlist, or checkout values.

Usage

import { useWishlist } from "react-use-wishlist";

const { metadata } = useWishlist();

Contributors āœØ

This Package is created by using the notrab/react-use-cart git repo. So, thanks goes to (notrab/react-use-cart) and all of it's contributors.