Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

any-hooks

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

any-hooks

Resolve or switch hooks

  • 4.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
17
increased by112.5%
Maintainers
1
Weekly downloads
 
Created
Source

any-hooks Build Status unstable

Resolve available hooks. Hooks are detected from installed hook libraries, if no hooks found - manual setup is expected.

NPM

import { useState, useEffect } from 'any-hooks'

export function useMyHook(init) {
  let [state, setState] = useState(init)
}

Detected libraries:

Set hooks manually:

import hooks from 'tng-hooks'
import setHooks, { useState, useEffect } from 'any-hooks'

// switch global hooks to custom hooks lib, like tng-hooks
setHooks(hooks)

Supported hooks

ReactPreactRaxHauntedAugmentorFucoAtomicoTNG-hooksfn-with-hooks
useState
useEffect
useContext✅*
useCallback
useReducer
useMemo✅*
useRef
useLayoutEffect
useImperativeHandle
useDebugValue*
useTransition*✅*
useProperty*

* − non-standard

Use cases

Hooks libraries / packages

Any-hooks can be used in hooks libraries (like unihooks) to extend frameworks support to non-react.

// super-hooks
import { useState, useEffect } from 'any-hooks'

export function useMySuperHook(init) {
  let [state, setState] = useState(init)

  // ...

  return [state, setState]
}

Hooks adapter

Any-hooks can enable react hooks for non-react libraries, like augmentor, haunted etc. The strategy is similar to preact/compat aliasing.

Aliasing in webpack

webpack.config.js:

const config = {
   //...
  "resolve": {
    "alias": {
      "react": "any-hook"
    },
  },
  // mute warnings
  "stats": {
    "warnings": false
  }
}
Aliasing in parcel

package.json:

{
  "alias": {
    "react": "any-hooks"
  },
}
Aliasing in babel

.babelrc:

{
  "plugins": [
    ["module-resolver", {
      "alias": {
        "react": "any-hooks"
      }
    }]
  ]
}
Aliasing in rollup

rollup.config.js:

import alias from '@rollup/plugin-alias'

module.exports = {
  //...
  plugins: [
    alias({
      entries: {
          react: 'any-hooks'
      }
    })
  ]
}
Aliasing in jest

jest.config.js:

{
  // ...
  "moduleNameMapper": {
    "react": "any-hooks"
  },
}
Aliasing via browserify

npm i -D aliasify, then in package.json:

{
  "aliasify": {
    "aliases": {
      "react": "any-hooks"
    }
  },
}
Aliasing in node

npm i -D module-alias, then:

var moduleAlias = require('module-alias')
moduleAlias.addAliases({ 'react': 'any-hooks' })

See also

  • enhook − turn function into hooks-enabled function.
  • unihooks − multiframework hooks collection.
  • any-observable − resolve any installed Observable.
  • any-promise − resolve any installed Promise.

License

MIT

HK

Keywords

FAQs

Package last updated on 04 Feb 2020

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc