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

@bigab/use-related-state

v0.0.1

Published

A React Hook for relating UI state to a list of entities

Downloads

8

Readme

useRelatedState()

A React Hook for relating UI state to a list of entities

Build Status

A Custom React Hook for relating UI state to a list of entities

Sometimes, when you are creating a component, that component will recieve an array from props, a list of things that represent some entity: a list of todos, people, definitions, etc..., and your UI Element may want to save a little bit of state for each of the Entities.

Somtimes it's state such as whether the definition box is open or closed, or whether the item has been selected, or the position it's been dragged to on the screen.

Putting the "extra bit of UI state" (a.k.a. the "Related State") directly on the entity feels wrong, and so this is a simple hook, that takes a list (an array) of entities and returns a list of tuples (also arrays), where the first item is just the item, and the second item is the related state, along with some functions to set the related state for the items.

Example

import useRelatedState from 'use-related-state'

const SpeakersList = ({ speakers }) => {
  const [speakerTuples, setSpeakerOpen] = useRelatedState(speakers, () => false)

  return speakerTuples.map(([speaker, isOpen]) => (
    <details key={speaker.id} open={isOpen}>
      <summary>{speaker.name}</summary>
      {speaker.bio}
    </details>
  )
}

Installation

npm install use-related-state

*Note use-related-state requires react as a peer dependancy

API

const [itemStateTuples, setRelatedStateForItem, setRelatedStateForAll] = useRelatedState( items:array(any)[, defaultValueMapper:function, identifier] )

Arguments

useRelatedState( items, defaultValueMapper, identifier )

  • items - an array of anything
  • defaultValueMapper [optional] - a function which recieves and item as an argument and returns the initial value of the related state before any related-state has been updated
  • identifier [optional] - a function used to create an identifier "key" to identify the entity. It defaults to v => v but if your entity objects are not always the same references, but just data representing those entities, you probably want to pass something like item => item.id for the identifier function

Returns

[itemStateTuples, setRelatedStateForItem, setRelatedStateForAll]

  • An array with 3 elements
    • itemStateTuples - an array of arrays, where each array element has 2 elements, [item, relatedState], the item item in the 0 index position and the relatedState in the 1 index position
    • setRelatedStateForItem - a function that takes 2 arguments (item, newRelatedState), if you need access to the old state, you can pass a function for newRelatedState which will be called with the current related state as an argument, and should return the value of the new related state
    • setRelatedStateForAll - a function that will be used to map over all the items and return the newRelated state you want to set. It takes a function as an argument, mapRelatedState, and that function will recieve the item and current relatedState and you should return the new related state for each item in the items list

TODO: Examples

Changelog

See the latest releases on GitHub.

License

MIT