@backhooks/core
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

Backhooks Core

This package holds the core of backhooks.

Get started

npm install @backhooks/core

Usage

createHook: (options) => [hookFunction, hookConfigurationFunction]

This allows you to create a hook. Example of creating a simple hook:

const [useCount, configureCountHook] = createHook({
  data() {
    return {
      count: 0,
    };
  },
  execute(state) {
    state.count++;
    return state.count;
  },
});

runHookContext: <T>(fn: () => T): Promise<T>

This allows you to run a hook context. Any hook called within the runHookContext callback will have a specific state attached to it:

runHookContext(() => {
  console.log(useCount()); // 1
  console.log(useCount()); // 2
  console.log(useCount()); // 3
});

runHookContext(() => {
  console.log(useCount()); // 1
  console.log(useCount()); // 2
  console.log(useCount()); // 3
});

resetGlobalContext()

This allows you to reset the global context. It can be very useful for testing purposes

beforeEach(() => {
  resetGlobalContext();
});

test("it should count", async () => {
  const count = useCount();
  expect(count).toBe(1); // true
});

test("it should also count", async () => {
  const count = useCount();
  expect(count).toBe(1); // also true
});

Dependents (5)

Package Sidebar

Install

npm i @backhooks/core

Weekly Downloads

7

Version

0.3.0

License

none

Unpacked Size

16.6 kB

Total Files

12

Last publish

Collaborators

  • coderhammer
  • hammerbot