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

react-use-wishlist

v1.0.1

Published

React hook library for managing wishlist state.

Downloads

147

Readme

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

| Prop | Required | Description | | -------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | | id | No | id for your wishlist to enable automatic wishlist retrieval via window.localStorage. If you pass a id then you can use multiple instances of WishlistProvider. | | onSetItems | No | Triggered only when setWishlistItems invoked. | | onItemAdd | No | Triggered on items added to your wishlist | | onItemRemove | No | Triggered on items removed from your wishlist. | | storage | No | Must return [getter, setter]. | | metadata | No | Custom 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.