Skip to content

A custom React hook to set/get state using a single function.

License

Notifications You must be signed in to change notification settings

welingtonms/use-value

Repository files navigation

use-value

Coverage Status npm package

A custom React hook that wraps the existing useState to be used similarly to jQuery's .val().

Usage

import { useValue } from '@welingtonms/use-value';

function CollapseSample() {
  const collapsed = useValue(false);

  return (
    <div>
      {collapsed() ? 'ON' : 'OFF'}
      <button
        type="button"
        onClick={() => {
          collapsed(!collapsed());
        }}
      >
        Toggle like this
      </button>
      <button
        type="button"
        onClick={() => {
          collapsed((isCollapsed) => !isCollapsed);
        }}
      >
        Or like this
      </button>
    </div>
  );
}