# @weebuinc/react-hooks

> Weebu React Hooks

Latest version **0.1.0** (published 2023-09-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install @weebuinc/react-hooks
pnpm add @weebuinc/react-hooks
yarn add @weebuinc/react-hooks
bun add @weebuinc/react-hooks
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2023-09-14 |
| First published | 2023-09-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 48.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Joseph Eniojukan |
| Maintainers | joejukan |
| Keywords | Weebu, React, Hooks |

## Links

- npm: https://www.npmjs.com/package/@weebuinc/react-hooks
- Repository: https://github.com/weebuinc/react-hooks
- Homepage: https://weebuinc.github.io/react-hooks/
- Issues: https://github.com/weebuinc/react-ui/issues
- npm.io page: https://npm.io/package/@weebuinc/react-hooks

## Dependencies (1)

- [@weebuinc/web-kit](https://npm.io/package/@weebuinc/web-kit.md) 0.1.2

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2023-09-14

## README

# Weebu React Hooks

This is a collection of very useful hooks provided by our company `Weebu Inc.`.

## Getting Started

<br/>

### Install Weebu's React Hooks

```
npm install @weebuinc/react-hooks
```

### Import the Hooks into your Project

```typescript
import axios from 'axios';
import { useState } from 'react';
import { useDebounce } from '@weebuinc/react-hooks';

export function SearchInput() {
  const [value, setValue] = useState('');
  const [names, setNames] = useState(new Array<string>());

  const search = useDebounce(async (name: string) => {
    // this logic will be called 300ms after the last dispatch of the search function
    const { data } = await axios.get('http://api.example.com/search', { params: { name } });
    setNames([...data]);
  });

  return (
    <div>
      <input
        value={value}
        onChange={(ev) => {
          setValue(ev.target.value);
          search(ev.target.value);
        }}
      />
      <ul>
        {names.map((name) => (
          <li key={name}>{name}</li>
        ))}
      </ul>
    </div>
  );
}
```

<br/>

## Available Hooks

Below are brief description of our available hooks. <br/>

### useClosure

The closure hook returns a reference that updates, automatically, by the dependencies passed to it.

It can be thought as a combination of the `useRef` and `useMemo` hooks.   Where the object returned by `useClosure` is a reference that is constantly updated based on the changes of the dependencies.

```typescript
import { useEffect, useState } from 'react';
import { useClosure } from '@weebuinc/react-hooks';

export function MyComponent({ onChange, listenToChange = false }) {
  const [mounted, setMounted] = useState(false);
  const [value, setValue] = useState('');

  const closure = useClosure(() => ({ listenToChange, mounted }), [listenToChange, mounted]);

  useEffect(() => {
    setMounted(true);
  }, []);

  useEffect(() => {
    // closure allows us to get the latest states of listenToChange and mounted without reacting to their state change.
    if (closure.current.listenToChange || closure.current.mounted) {
      onChange(value);
    }
  }, [value]);

  return (
    <input value={value} onChange={(ev) => setValue(ev.target.value)} />
  )
}
```

### useDebounce

The debounce hook takes in a callback function and returns a dispatch function that debounces the callback.

```typescript
import axios from 'axios';
import { useState } from 'react';
import { useDebounce } from '@weebuinc/react-hooks';

export function SearchInput() {
  const [value, setValue] = useState('');
  const [names, setNames] = useState(new Array<string>());

  const search = useDebounce(async (name: string) => {
    // this logic will be called 300ms after the last dispatch of the search function
    const { data } = await axios.get('http://api.example.com/search', { params: { name } });
    setNames([...data]);
  });

  return (
    <div>
      <input
        value={value}
        onChange={(ev) => {
          setValue(ev.target.value);
          search(ev.target.value);
        }}
      />
      <ul>
        {names.map((name) => (
          <li key={name}>{name}</li>
        ))}
      </ul>
    </div>
  );
}
```

### usePatch

The patch hook allows an object state to have some (or all) of its fields to be changed by the dispatch setter.

```typescript
import { usePatch } from '@weebu/react-hooks';

export function MyComponent() {
  // allows for the firstName to be changed without affecting the lastName and vice versa
  const [patch, setPatch] = usePatch({ firstName: '', lastName: '' });
  return (
    <>
      <input
        name='firstName'
        value={patch.firstName}
        onClick={(ev) => setPatch({ firstName: ev.target.value })}
      />
      <input
        name='lastName'
        value={patch.lastName}
        onClick={(ev) => setPatch({ lastName: ev.target.value })}
      />
    </>
  );
}
```

---
_Source: https://npm.io/package/@weebuinc/react-hooks · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
