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

@caasi/hooks

v1.7.5

Published

Some useful React Hooks

Downloads

37

Readme

NPM Status Build Status

@caasi/hooks

Some heretical React Hooks.

Install

npm install @caasi/hooks

Core hooks

useConcat

Yields one value or another.

const x = useConcat(0, 1); // yields 0 and 1

useArray

Yields every values from a given array.

const xs = useMemo(() => [0, 1, 2], []);
const x = useArray(xs); // yields 0, 1 and 2

useGenerator

Treats a generator as a stream and collects values from it.

function* gen() {
  yield 0;
  return 1;
}

const iter = useMemo(() => gen(), []);
const [value, done] = useGenerator(iter);

useDefined

Filters out undefined values.

function* gen() {
  yield 0;
  yield undefined;
  return 1;
}

const iter = useMemo(() => gen(), []);
const [value, done] = useGenerator(iter);
const x = useDefined(value); // yields 0 and 1

useFold

Folds over a hook value.

function* gen() {
  yield 1;
  yield 2;
  return 3;
}

const iter = useMemo(() => gen(), []);
const [value, done] = useGenerator(iter);
const x = useFold(value, (a, b) => a + b, 0); // yields 0, 1, 3 and 6

useFilter

Filters out unwanted values.

const mod2 = useCallback(x => x % 2 === 0);
const as = useMemo(() => [0, 1, 2], []);
const x = useArray(as);
const y = useFilter(x, mod2); // yields 0 and 2

usePromise

Resolves a Promise and returns its status.

const p = useMemo(() => api.get('https://example.com'), []);
const [value, error, isPending] = usePromise(p);

useMaybe/useOptional

Chains optional values into another optional value.

const ab = useMaybe(
  [a, b],
  (a, b) => a * a + b * b,
);

IO hooks

useTime

Gives a DOMHighResTimeStamp diff from the time it was called first time.

const t = useTime();

useRange

Gives a number between start and end. It's an application of useTime and it's useful in simple animations.

const theta = useRange(0, 2 * Math.PI);
const x = Math.cos(theta);
const y = Math.sin(theta);
const pt = { x, y };

useImageData

Reads an image from a URL and gives you an ImageData.

const imageData = useImageData('https://example.com/lena.png');

useBlob/useFile

Reads a Blob as an ArrayBuffer or a string. Defaults to an ArrayBuffer.

enum ResultType {
  ARRAY_BUFFER = 'arraybuffer',
  BINARY_STRING = 'binarystring',
  DATA_URL = 'dataurl',
  TEXT = 'text',
}

const dataurl = useBlob(file, useBlob.ResultType.DATA_URL);

useObjectURL

Creates an object URL from anything. It's useful with an image blob.

const imageData = useImageData(useObjectURL(file));

useImageFile

Gives a ImageData from an image blob. It's an alias of file => useImageData(useObjectURL(file)).

useWebSocket

Opens a web socket and streams messages.

const [socket, messages = []] = useWebSocket('wss://echo.websocket.org');
const msgs = messages.filter(x => x).reverse();

UI component related hooks

useProp

Binds a state and a state handler to a React element.

const [val, elem] = useProp(element, value, 'value', 'onChange', e => e.target.value);

useInput

A shortcut to bind a value to an input element.

const [r, rRange] = useInput(
  <input type="range" min="0.0" max="1.0" step="0.01" />,
  '1.0',
)

Other hooks

useSpace

Stores state histories.

It's named as useSpace instead of useHistory because it flattens a value in time to space(a list).

const [state, setState] = useState(0);
const history = useSpace(state);

useEffect(() => {
  setState(s => s + 1);
  console.log(history);
}, [history]);

ToDo

  • [x] test custom hooks with cypress
  • [x] add useDefined to keep the previous defined value
  • [x] remove .github
  • [x] implement useFold
    • [x] implement useFilter
  • [x] stop using undefined as a reset signal in useSpace
  • [ ] test more hooks
    • [ ] useConcat
    • [ ] useImageData
    • [ ] useImageFile
    • [ ] useProp
    • [ ] useInput
    • [ ] useObjectURL
    • [ ] useTime
    • [ ] useRange
    • [x] useWebSocket
  • [ ] rewrite everything in TypeScript