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

tachyon-utils-react

v32.0.0

Published

SSR-safe custom hooks and other React functionality

Downloads

3

Readme

tachyon-utils-react

Custom React functionality for SSR.

yarn add tachyon-utils-react

Available Hooks

  • useAsync
  • useCallbackMemoizedByKey
  • useConst
  • useConstCallback
  • useDebounce
  • useDebouncedState
  • useDelayed
  • useEffectOnce
  • useEvent
  • useForceUpdate
  • useInterval
  • useJSEnabled
  • useLatest
  • useMethods
  • useMountedState
  • usePoll
  • usePrevious
  • useStorage
  • useThrottle
  • useTimeout
  • useUnmount

Adding New react-use Hooks

If you'd like to add another react-use hook to this package, reach out in #tachyon-lib-support so we can ensure it is safe to use and compatible with the Tachyon platform.

Usage Notes

useConst

Returns a stable value on each render based on the value passed to the hook or the value returned by calling the passed function on initial render. Any argument changes beyond the first render are ignored.

useConstCallback

Convenience helper for returning stable callbacks which is equivalent to calling useConst(() => callback). This is also similar to what the built-in useCallback provides when the dependency list is empty, however, useMemo and useCallback don't guarantee a stable value even with an empty dependency list but this version does.

useLatest

Version of useLatest which is safe for future concurrent mode rendering. Instead of updating the ref's current value during render, it is updated in the commit phase via useLayoutEffect.

This has the minor side effect of the .current property actually being the previous value during render except on the first render, but if the current value is needed during render the value passed to this hook can simply be used elsewhere directly.

useMethods

Similar to useMethods from react-use but the create factory function is called only once and is passed a dispatch function which behaves like the setter returned from useState. This allows the use of async methods which dispatch multiple times without making the simple cases any more complex (just call dispatch in place of a return).

Additionally, this version supports an optional 3rd argument whose value will be saved to a ref and passed as a second parameter to the create function, allowing the returned methods to access the current value without needing to explicitly push this value into state (thus saving a re-render).

The react-use example would look like this:

import type { MethodDispatch } from 'tachyon-utils-react';
import { useMethods } from 'tachyon-utils-react';

const initialState = {
  count: 0,
};

function createMethods(dispatch: MethodDispatch<typeof initialState>) {
  return {
    reset() {
      dispatch(initialState);
    },
    increment() {
      dispatch((state) => ({ ...state, count: state.count + 1 }));
    },
    decrement() {
      dispatch((state) => ({ ...state, count: state.count - 1 }));
    },
  };
}

const Demo = () => {
  const [state, methods] = useMethods(createMethods, initialState);

  return (
    <>
      <p>Count: {state.count}</p>
      <button onClick={methods.decrement}>-</button>
      <button onClick={methods.increment}>+</button>
    </>
  );
};