🚀 Launch Week Day 4:Introducing the Alert Details Page: A Better Way to Explore Alerts.Learn More →
Socket
Book a DemoInstallSign in
Socket

@saasquatch/universal-hooks

Package Overview
Dependencies
Maintainers
11
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@saasquatch/universal-hooks

An interface and a swappable implementation that can be used to implement universal custom hooks on top of hooks.

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
11
Created
Source

Universal Hooks

This library aims to provide a common interface for React-like hooks to plug into. Simply plug in an implementation and your hooks are reusable across different implementations.

Usage

There are two ways to plug in an implementation. You can supply a module at runtime or you can replace Universal Hooks at compile time.

Provide an implementation at runtime

// Set an implementation for the hooks
setImplementation(haunted)
// OR
setImplementation(React)
// OR
setImplementation(Preact)

// Use the hooks like normal
function useCounter() {
    const [counter, setCounter] = useState(0);

    const increment = () => setCounter((c) => c + 1)
    const decrement = () => setCounter((c) => c - 1)

    return {counter, increment, decrement}
}

Provide an implementation via bundlers

Rollup, Webpack and other bundlers allow compile-time replacement of implementations.

E.g. with the Rollup alias plugin https://github.com/rollup/plugins/tree/master/packages/alias

To swap in React

entries: [
  { find: '@saasquatch/universal-hooks', replacement: 'React' },
];

To swap in Haunted

entries: [
  { find: '@saasquatch/universal-hooks', replacement: 'haunted' },
];

API

Most common hooks are supported, except for useContext.

  • useState
  • useMemo
  • useCallback
  • useEffect
  • useReducer
  • useRef

FAQs

Package last updated on 20 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts