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

@xiel/outer-hooks

v0.0.5

Published

Lorem ipsum

Downloads

19

Readme

OuterHooks 💫

npm (tag) GitHub top language

Create function components using powerful and composable hooks.

If you know and love hooks from React, you already know the main API of OuterHooks as they are very alike.

Differences to React

  • OuterHooks is plain JavaScript/TypeScript
  • No JSX here – does not render to the DOM or any UI really
  • Only compose logic using native & custom hooks
  • Life cycle hooks like useEffect also run in Node

Install

yarn add @xiel/outer-hooks@beta

Warning: Work in progress 🚧

This library is still in beta. It works, is well tested and actively being developed. But the API is not 100% stable yet.

Hooks

Native Hooks

  • useState, useRef, useMemo, useCallback, useReducer, useEffect, useLayoutEffect, ...

For now please check the React Hooks API reference as they work exactly the same in OuterHooks.

Custom Hooks

By composing native hooks, you are creating a custom hook. Native hooks and custom hooks can be composed and nested.

Example

// Define a custom hook
function useCustomHook() {
  const [currentState, setState] = useState(0)

  useEffect(() => {
    if (currentState < 5) {
      setState(currentState + 1)
    }
  }, [currentState])

  return currentState
}

// Run the hook
const custom = runHook(useCustomHook)

// Gets called every time the hook has run
// In this example it will get called with the values 0, 1, 2, 3, 4, 5
custom.on('update', (value) => console.log(value))

// Gets called when the hook was destroyed
custom.on('destroy', (error) => console.error('destroyed'))

runHook(fn)

runHook(fn) returns the following interface, which lets you await the next value, await effects, read the latest value and subscribe to updates to your hook.

export interface Root<Props, HookValue> {
  displayName: string

  /**
   * Resolves once the hooks has rendered.
   * Might resolve after being intermediately suspended.
   */
  value: Promise<HookValue>

  /**
   * Resolves once all side effects have run (cleanups, useLayoutEffects and useEffects)
   */
  effects: Promise<void>

  /**
   * Returns the current value of the ran hook (outermost custom `useXYZ` hook)
   * This might return undefined or a stole/older value while the hook is suspended.
   * Recommended: Use the value promise to get the latest value.
   */
  currentValue?: HookValue

  /**
   * While the hook is suspended, this will return true
   */
  isSuspended: boolean

  /**
   * If the hook was destroyed (by error or externally), this will return true
   */
  isDestroyed: boolean

  /**
   * Resolves after all cleanup functions have run
   */
  isDestroyedPromise: Promise<unknown> | undefined

  /**
   * Re-run the hook with new props
   */
  render: RenderFn<Props, HookValue>

  /**
   * Re-run the hook with (partially) new props.
   */
  update: UpdateFn<Props, HookValue>

  /**
   * Subscribe to hook updates
   */
  on: <T extends keyof SubscriptionTypes>(
    type: T,
    subscription: SubscriptionTypes<HookValue>[T]
  ) => UnsubscribeFn

  /**
   * Unsubscribe from hook updates
   */
  off: <T extends keyof SubscriptionTypes>(
    type: T,
    subscription: SubscriptionTypes<HookValue>[T]
  ) => void

  /**
   * Destroy the hook.
   * This will run all cleanup functions and reject the value promise
   */
  destroy(reason?: unknown): Promise<unknown>
}