0.0.9 • Published 10 months ago

nohooks v0.0.9

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

nohooks

Compose reactive hooks without additional libraries.

Installation

$ npm i nohooks --save

How it works?

The nohooks library provides a single module with several low-level hooks, e.g.

import { createContext, useState } from 'nohooks';

async function main() {
  const scope = createContext(() => {
    const [value, setValue] = useState(3);
    if (value > 0) setValue(value - 1);
    console.log(value);
    return value;
  })();

  console.log(scope.result === 3);
  await scope.defer();
  console.log(scope.result === 0);
}
main();

Notice scope.result returns the initial value immediately, after waiting it returns the last computed value.

Using context

Calling createContext(render[, cb]) will return a function later used to compute values.

It also accepts a second argument that is called to set the scope.set method, for triggering updates.

Available hooks

  • onError(cb) Capture unhandled exceptions.
  • useMemo(cb[, deps]) Memoized callback result.
  • useEffect(cb[, deps]) Fires a synchronous callback.
  • useRef([defaultValue]) Returns a persistent unique reference.
  • useState([defaultValue]) Returns a value/setter from the any given value.

Notice that no passing deps will trigger the given callback on every iteration, use [] to fire it once.

Other utilities

  • clone(obj) Returns a copy from any given value.
  • equals(a, b) Returns true if a and b are equal.
0.0.9

10 months ago

0.0.8

11 months ago

0.0.7

1 year ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.6

2 years ago

0.0.1

3 years ago