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

@clave/use-array-state

v1.3.0

Published

> Make it easier to work with stateful arrays

Downloads

67

Readme

useArrayState

Make it easier to work with stateful arrays

Documentation

npm install @clave/use-array-state

The goal of this hook is to make it easier to manipulate array state. For example, if you want to add a new item to an array you can call setState.append(value).

Usage

import useArrayState from "@clave/use-array-state";

// useArrayState instead of useState
const [items, setItems] = useArrayState(["a", "b", "c"]);

console.log(items); // ["a", "b", "c"]

// setItems behaves like normal
setItems(["x", "y", "z"]); // ["x", "y", "z"]

// Append an item to the end of the array
setItems.append("d"); // ["a", "b", "c", "d"]

// Append multiple items to the end of the array
setItems.append("d", "e", "f"); // ["a", "b", "c", "d", "e", "f"]

// Prepend an item to the beginning of the array
setItems.prepend("x"); // ["x", "a", "b", "c"]

// Prepend multiple items to the beginning of the array
setItems.prepend("x", "y", "z"); // ["x", "y", "z", "a", "b", "c"]

// Insert an item somewhere in the array
setItems.insertAt(1, "x"); // ["a", "x", "b", "c"]

// Insert multiple items somewhere in the array
setItems.insertAt(1, "x", "y", "z"); // ["a", "x", "y", "z", "b", "c"]

// Remove a value from the list
setItems.remove("b"); // ["a", "c"]

// Remove several values from the list
setItems.remove("b", "a"); // ["c"]

// Remove the value at specified index
setItems.remove(1); // ["a", "c"]

setItems.removeWhere((v) => v === "b"); // ["a", "c"]

// Edit the value at a specified index
setItems.editAt(1, (v) => v.ToUpperCase()); // ["a", "B", "c"]

// Replace the value at a specified index
setItems.replaceAt(1, "x"); // ["a", "x", "c"]

// Replace a value with another value
setItems.replace("b", "x"); // ["a", "x", "c"]

Alternative usage

If you worry about the size of the package with all these, potentially unnecessary, methods, then there is an alternative way to use it. All of the methods are exported from the module, so you can import each one individually and use only the ones you need:

import { useState } from "react";
import { append, removeAt } from "@clave/use-array-state";

export default function MyComponent(){
  const [items, setItems] = useState([]);

  return (
    <button onClick={() => setItems(append("some value"))}>
    {items.map((value, index) => (
      <div>
        {value}
        <button onClick={() => setItems(removeAt(index))}>
      </div>
    ))}
  )
}

That is, instead of using setItems.something(args) you can call setItems(something(args)) if you add import { something } from "@clave/use-array-state";. This will let the tree-shaker exclude all unused methods.