any-hooks
Resolve available hooks. Hooks are detected from installed hook libraries, if no hooks found - manual setup is expected.
{ let state setState = }
Detected libraries:
-
react
-
preact
-
rax
-
haunted
-
fuco
-
augmentor
(dom-augmentor
,neverland
) -
atomico
-
tng-hooks
(manual) -
fn-with-hooks
(manual) -
plumejs
(manual) -
component-register-hooks
(manual)
Set hooks manually:
// switch global hooks to custom hooks lib, like tng-hooks
Supported hooks
React | Preact | Rax | Haunted | Augmentor | Fuco | Atomico | TNG-hooks | fn-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 { let state setState = // ... 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
:
Aliasing in babel
.babelrc
:
"plugins": "module-resolver" "alias": "react": "any-hooks"
Aliasing in rollup
rollup.config.js
:
moduleexports = //... plugins:
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 = moduleAlias
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