3.1.1 • Published 4 years ago

enhook v3.1.1

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

enhook Build Status unstable

Enable react/preact/∀ hooks for regular functions.

NPM

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

let countFrom = enableHooks(initCount => {
  let [count, setCount] = useState(initCount)

  setTimeout(() => {
    setCount(++count)
  }, 1000)

  // any side-effects
  useEffect(() => console.log(count), [count])
})

countFrom(0)

Enhook turns any function into reactive function with enabled hooks for a given framework. The framework is by default detected from the list:

In case of ES modules autodetection is not available (until import.meta.resolve() or await import() is available), you have to manually indicate framework to use.

import * as preact from 'preact'
import preactHooks from 'preact/hooks'
import enhook from 'enhook'
import setHooks, { useState } from 'any-hooks'

enhook.use(preact) // or enhook.use(React, ReactDOM)
setHooks(preactHooks)

// now enhook uses preact with  as base
let fn = enhook(() => {
  let [count, setCount] = useState(0)
  //...
})

API

fn = enhook(fn, { passive=false }?)

Create function wrapper, allowing hooks in function body. passive option may define if function must be reactive.

import enhook from 'enhook'
import { useState } from 'any-hooks'

let passiveFn = enhook((i) => {
  let [count, setCount] = useState(0)

  // this does not cause self-recursion in passive mode
  setCount(i)
}, { passive: true })

passiveFn(1)
passiveFn(2)

fn.unhook()

Teardown enhooked function. This will dispose all useEffects. Any subsequent calls to that function will throw an error.

See also

  • unihooks - unified all-framework essential hooks collection.

License

MIT

3.1.1

4 years ago

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.6.4

4 years ago

1.6.3

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.0

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago