# t-hooks

> ![CI](https://github.com/lammonaaf/t-hooks/workflows/CI/badge.svg)

Latest version **2.1.1** (published 2026-10-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install t-hooks
pnpm add t-hooks
yarn add t-hooks
bun add t-hooks
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.1.1 |
| Published | 2026-10-16 |
| First published | 2020-07-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18 |
| Dependencies | 1 |
| Unpacked size | 126.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Alexander Yushkov |
| Maintainers | lammonaaf |
| Keywords | react, hooks, react-hooks, tasks, flow, monad, cancelable |

## Links

- npm: https://www.npmjs.com/package/t-hooks
- Repository: https://github.com/lammonaaf/t-hooks
- Homepage: https://lammonaaf.github.io/t-hooks
- Issues: https://github.com/lammonaaf/t-hooks/issues
- npm.io page: https://npm.io/package/t-hooks

## Dependencies (1)

- [t-tasks](https://npm.io/package/t-tasks.md) ^2.1.1

## 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

- 2.1.1 (latest) — 2026-10-16
- 2.1.0 — 2026-10-12
- 2.0.0 — 2026-09-29
- 1.2.4 — 2026-04-19
- 1.2.3 — 2026-04-19
- 1.2.2 — 2026-04-19
- 1.2.1 — 2026-04-19
- 1.2.0 — 2026-04-18
- 1.1.3 — 2025-11-27
- 1.1.2 — 2025-11-27
- 1.1.0 — 2024-02-06
- 1.0.1 — 2022-07-21
- 1.0.0 — 2022-01-17
- 1.0.0-beta.2 — 2022-01-17
- 1.0.0-beta.1 — 2022-01-17
- … 16 more at https://npm.io/package/t-hooks/versions

## README

![CI](https://github.com/lammonaaf/t-hooks/workflows/CI/badge.svg)

# T-Hooks task-based React hook library

T-Hooks provides a set of react hooks built upon T-Tasks library. Using task hooks instead of conventional lifecycle hooks alows easier usage of asynchronous operations withing hooks and provides automatic operation cancelation in case of hook unmounting or re-render.

### Task-based useEffect

In this scenario, every time ```arg``` updates an asynchronous operation is started, resulting in a side effect. If hook is unmonted before the operation finishes no side effect is performed, preventing modification of state of unmounted component. Moreover, if ```arg``` changes before the operation finishes another operation in started instead, canceling the previous one.

```tsx
const taskCreator = (arg: string) => Task.fromPromise(someAsyncOperation(arg)).tap((result) => {
  setSomething(result); // side effects
});

useTaskEffect(() => taskCreator(arg), [arg]);
```

### Generator-based useEffect

In this scenario a generator syntax is used to achieve the same result. Generator syntax allows for more natural task chaining via ```yield``` syntax. Note that despite the fact that a task may be yielded directly as ```yield someTask``` the result of such expression may be unknown in case of multiple yield statements, so using ```yield* someTask.generator()``` is recommended.

```tsx
useGeneratorEffect(function*() {
  const result = yield* Task.fromPromise(someAsyncOperation(arg)).generator();

  setSomething(result); // side effects
}, [arg]);
```

### Chaining multiple effects

In this scenario generator syntax is used to chain two consequtive async operations with two side-effects. In case of unmounting or re-render during execution of the first operation, the second one would not be started and both side effects would not be performed. In case of unmounting or re-render during execution of the second operation, only the second side-effect would be prevented. 

```tsx
useGeneratorEffect(function*() {
  const result = yield* Task.fromPromise(someAsyncOperation(arg)).generator();

  setSomething(result1); // side effect 1

  const result2 = yield* Task.fromPromise(otherAsyncOperation(resilt1)).generator();

  setSomethingElse(result2); // side effect 2
}, [arg]);
```

### Task-based useMemo

In this scenario, every time ```arg``` updates an asynchronous operation is started, updating ```userData``` asynchronously. In case of hook unmounting or updating ```arg``` again before the previous operation finishes, another operation in started instead, canceling the previous one.

```tsx
const userData = useTaskMemo(null, () => Task.fromPromise(getUserDataAsync(userId)), [userId]);
```

### Generator-based useMemo

In this scenario a generator syntax is used to achieve the same result. Generator syntax allows for more natural task chaining via ```yield``` syntax. Note that despite the fact that a task may be yielded directly as ```yield someTask``` the result of such expression may be unknown in case of multiple yield statements, so using ```yield* someTask.generator()``` is recommended.

```tsx
const userData = useGeneratorMemo(null, function*() {
  return yield* Task.fromPromise(getUserDataAsync(userId)).generator();
}, [userId]);
```

### Chaining multiple transformations

In this scenario a generator syntax is used to chain two consequtive async operations in a single task memo.

```tsx
const usageStats = useGeneratorMemo(null, function*() {
  const userData = yield* Task.fromPromise(getUserDataAsync(userId)).generator();

  yield* timeoutTask(100).generator(); // one may add delays easily

  return yield* Task.fromPromise(getUsageStats(userData)).generator();
}, [userId]);
```

### Task-based useCallback

In this scenario an asynchrounous callback is created, performing side-effects only if the hook is not unmounted. Callback is also interrupted if invoked again

```tsx
const taskCreator = (userId: string) => liftResult(getUserDataAsync(arg)).tap((userData) => {
  setUserData(userData); // side effects
});

const onClick = useTaskCallback(() => taskCreator(userId)), [userId]);

return <button onClick={onClick}>Click me</button>
```

### Generator-based useCallback

In this scenario an asynchrounous callback is created via generator syntax

```tsx
const onClick = useGeneratorCallback(function*() {
  const userData = yield* Task.fromPromise(getUserDataAsync(userId)).generator();

  yield* timeoutTask(100).generator(); // one may add delays easily

  setUserData(userData); // side-effects
}), [userId]);

return <button onClick={onClick}>Click me</button>
```

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