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-set

v1.0.0

Published

Use Set-like API with React hook

Downloads

235,340

Readme

react-use-set

Use Set-like API with React hook.

npm CI Status codecov semantic-release: angular

Instsall

Install react-use-set npm package

npm i react-use-set

Usage

Get a Set-like object from useSet() hook.ß

import { useSet } from "react-use-set"

const set = useSet()

It's got some Set methods and properties like

  • add (enhanced)
  • delete (enhanced)
  • clear
  • size

Along with additional utils like

  • toggle
  • sync
  • toArray

Learn more in API section.

API

set.add(...values)

Calling this method triggers a re-render.

Add values to the Set. Multiple values are supported.

const set = useSet([1, 2, 3])

set.add(4) // Set [1, 2, 3, 4]
set.add(5, 6) // Set [1, 2, 3, 4, 5, 6]

set.delete(...values)

Calling this method triggers a re-render.

Remove values from the Set. Multiple values are supported.

const set = useSet([1, 2, 3])

set.delete(1, 2) // Set [3]

set.has(value)

Check whether a value exists in Set.

const set = useSet([1, 2, 3])

set.has(1) // true
set.has(4) // false

set.toggle(value)

Calling this method triggers a re-render.

If value exists in the Set, remove it, otherwise add it.

const set = useSet([1, 2, 3])

set.toggle(2) // Set [1, 3]
set.toggle(4) // Set [1, 3, 4]

This is useful when you want to store selected options on a list.

const selectedValues = useSet()

options.map((option) => (
  <Checkbox onChange={() => selectedValues.toggle(option.value)} />
))

set.clear()

Calling this method triggers a re-render.

Remove all values from the Set.

const set = useSet([1, 2, 3])

set.clear() // Set []

set.size

Return the number of values in the Set.

const set = useSet([1, 2, 3])

set.size // 3

set.toArray()

Return the values in the Set as an array.

const set = useSet([1, 2, 3])

set.toArray() // [1, 2, 3]

This is useful when you want to display a list of selected options.

<ol>
  Selected options ({selectedValues.size})}):
  {selectedValues.toArray().map((value) => (
    <li key={value}>{value}</li>
  ))}
</ol>

set.sync(values)

Calling this method triggers a re-render.

Reset the Set with the given values.

const set = useSet([1, 2, 3])

set.sync([4, 5, 6]) // Set [4, 5, 6]

License

MIT © Doma