# use-optimistic-mutation

> >

Latest version **0.2.3** (published 2020-04-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-optimistic-mutation
pnpm add use-optimistic-mutation
yarn add use-optimistic-mutation
bun add use-optimistic-mutation
```

## Health

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

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

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.3 |
| Published | 2020-04-24 |
| First published | 2020-03-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 21.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Fredrik Johansson |
| Maintainers | frimjo |

## Links

- npm: https://www.npmjs.com/package/use-optimistic-mutation
- npm.io page: https://npm.io/package/use-optimistic-mutation

## Recent versions

- 0.2.3 (latest) — 2020-04-24
- 0.2.2 — 2020-04-24
- 0.2.1 — 2020-04-24
- 0.2.0 — 2020-04-21
- 0.1.0 — 2020-03-26
- 0.1.1 — 2020-03-26

## README

# use-optimistic-mutation

>

[![NPM](https://img.shields.io/npm/v/use-optimistic-mutation.svg)](https://www.npmjs.com/package/use-optimistic-mutation) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

## Install

```bash
npm install --save use-optimistic-mutation
```

## Usage

```tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { useQuery } from 'react-query';
import useOptimisticMutation from '../.';

interface IUser {
  name: string;
}

let USER_DB_MOCK: IUser = { name: 'Lisa' };

function getUser(): Promise<IUser> {
  return new Promise(resolve =>
    setTimeout(() => {
      resolve(USER_DB_MOCK);
    }, 1000)
  );
}

function updateUser(user: IUser): Promise<IUser> {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      USER_DB_MOCK = user;
      return resolve(USER_DB_MOCK);
    }, 1000);
  });
}

const App = () => {
  const { data, status } = useQuery(['user'], getUser);
  const [update] = useOptimisticMutation(['user'], updateUser);

  if (status === 'loading') {
    return <h5>Fetching name…</h5>;
  }

  if (data === undefined) {
    return <h5>No user received</h5>;
  }

  return (
    <div>
      <h4>Name: {data.name}</h4>
      <button onClick={() => update({ name: 'Marge' })}>
        Update name optimisticly
      </button>
    </div>
  );
};
```

## License

MIT © [FrimJo](https://github.com/FrimJo)

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