mediamonks / react-kit

A collection of React hooks, components and utilities we use at Media.Monks
https://mediamonks.github.io/react-kit/
MIT License
9 stars 2 forks source link
hooks react

npm version npm downloads

@mediamonks/react-kit

Collection of commonly used React hooks.

Getting started

Installing

Add @mediamonks/react-kit to your project:

npm i @mediamonks/react-kit

Example

Use a hook inside a component:

import { useToggle } from '@mediamonks/react-kit';

function DemoComponent() {
  const [state, toggle] = useToggle(false);

  return (
    <div>
      <div>{state} </div>
      <button onClick={() => toggle()}>Toggle</button>
    </div>
  );
}

Docs

https://mediamonks.github.io/react-kit/

Development

The information below should help you develop new hooks in this library.

Run npm run test -- --watch to run all unit tests in watch mode.

Run npm run storybook to preview your stories and documentation.

Folder Structure

useHookName

Steps for adding a new Hook:

Run the plop script and enter your hook name starting with use.

npm run plop

Which will execute the following steps, where you need to fill in the content.

Writing Unit test

Hooks can be tested using the renderHook function that now exists in @testing-library/react.

At the time of writing, this method is undocumented. It can be used as follows:

import { renderHook } from '@testing-library/react';

// init the hook
const { result, rerender, unmount } = renderHook(useToggle, {
  // values passed to your hook
  initialProps: { foo: 'bar' },
});

// inspect the response of the hook
console.log(result.current);

Run Component Lifecycle

To interact with your hook, you must use the act function.

import { act, renderHook } from '@testing-library/react';

// init the hook
const { result, rerender, unmount } = renderHook(useToggle, {
  // values passed to your hook
  initialProps: { foo: 'bar' },
});

// inspect the response of the hook
console.log(result.current);

act(() => {
  // interact with your hook
  result.current[1]();
});

// inspect the updated value of the hook
console.log(result.current);