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

@generalizers/react-utils

v3.0.1

Published

A react utils structure with some react helpers to make programming faster with react and typescript.

Downloads

2

Readme

react-utils

A react utils structure with some react helpers to make programming faster with react and typescript.

Document utils

Bind events to document with react

Document events in react can be hard sometimes. If we want to add an event to the document and detect changes in react, we have to do the following :

const f = () => console.log('click');

useEffect(() => {
  document.addEventListener('click', f);
  return () => {
    document.removeEventListener('click', f);
  };
}, []);

Basically we add the event to the document when the component mounts. And when it unmounts we remove the event. If we were to add states in here, we could either use state refs so the changes apply in any way or we could also use the useEffect dependencies.

const [count, setCount] = useState(0);

const f = () => setCount(count + 1);

useEffect(() => {
  document.addEventListener('click', f);
  return () => {
    document.removeEventListener('click', f);
  };
}, [count]);

If we didn't have the [count] in the useEffect, the function will never change. It will always consider the count variable as having a value of 0. Thus, setCount(count + 1) will always change the state to 1.

Document binding hooks

This library makes it easy to bind events to the document.

| hooks | parameters | description | | ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------- | | useKey | type: "down" | "press" | "up", f: (this: Document, ev: KeyboardEvent) => any, deps?: DependencyList | Bind a key event function to the document | | useMouse | type: "down" | "enter" | "leave" | "move" | "out" | "over" | "up" | "click", f: (this: Document, ev: KeyboardEvent) => any, deps?: DependencyList | Bind a mouse event function to the document | | useContextMenu | f: (e: Event) => any, deps?: DependencyList | Bind the contextmenu event function to the document |

Example

useMouse('down', () => {
  console.log('Mouse Down !');
});

State hooks

Arrays

We all know that it's hard to use react with arrays. Whenever you push a value to an array you always have to do a setState([...arr]) to update the state of the array.

With react-utils, you can use a react array hook to create your array and simply push the value in the state array and the array will update. It will internally call the setState function from the useState function.

Create a state array

The array state hook is a bit weird because you need to call a first function to get the actual hook. We know that it is weird but it is do to a typescript limitation on generic inferences. You can find the github issue here. We countered this limitation by using the typescript currying feature. This allows us to get intellisens while specifying the options for the useStateArray 'hook' (we'll just call the overall function a hook).

const arr = useStateArray()([1, 'Hello', false]);

return (
  <div onClick={() => arr.push('New value')}>
    {arr.map((e) => (
      <p>{e.toString()}</p>
    ))}
  </div>
);

stateArray1.png stateArray2.png

Selectable

Sometimes need to select an element to show it selected in the DOM. These are the states you would have in such a react application :

const [arr, setArr] = useState([1, 2, 3]);
const [selected, setSelected] = useState(0);

Here you have 4 variables used to identify an array and a selected element in the array.

In react-utils, all you need is to add a config object to the useStateArray hook :

const arr = useStateArray()([1, 'Hello', false], {
  selectable: { index: 1 },
});

return (
  <div>
    {arr.map((e) => (
      <p>{e.toString()}</p>
    ))}
    <p>{`selected : ${arr.selected.value}`}</p>
  </div>
);

stateArray3.png

You can also change the selection. Make sure not to change the selection in root of the component function (it will update in a loop since when a component re-renders it re-runs the component code)

arr.select(0);

Here are the available properties on arr when specifying the selectable option

| property | return | description | | ---------------- | --------------------------- | ---------------------------------- | | select(number) | void | Select a new element in the array | | selected | {index: number, value: T} | Get the currently selected element |

useStateArray options

| option | values | description | | ------------ | --------------------- | --------------------------------- | | selectable | {index?:number = 0} | The selected element in the array |

Distance

Sometimes you might need some advanced array features. The distance array is an array of numbers and will store the distances for each number in the array. At the current time, the distance array only supports doing the sum from the distance array that it creates

// [1,3,6] <=> [1,2,3]
const arr = useStateArray()([1, 3, 6], {
  distance: true,
});
arr.push(5);
// distances : [1, 2, 2, 3]
// arr : [1, 3, 5, 8]

The difference between 6 and 3 is 3. So the array will maintain a distance of 3 with it's previous element. This is why we go from 5 to 8.