Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mamal72/react-optimistic-ui-hook
⚛️ Minimal "optimistic UI" pattern implementation with a React Hook
https://github.com/mamal72/react-optimistic-ui-hook
frontend optimistic-ui react react-hooks ui ux
Last synced: about 1 month ago
JSON representation
⚛️ Minimal "optimistic UI" pattern implementation with a React Hook
- Host: GitHub
- URL: https://github.com/mamal72/react-optimistic-ui-hook
- Owner: mamal72
- License: mit
- Created: 2020-01-25T21:20:38.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T05:43:56.000Z (almost 2 years ago)
- Last Synced: 2024-07-31T07:15:38.276Z (4 months ago)
- Topics: frontend, optimistic-ui, react, react-hooks, ui, ux
- Language: TypeScript
- Homepage:
- Size: 1.87 MB
- Stars: 22
- Watchers: 3
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- fucking-awesome-react-hooks - `react-optimistic-ui-hook`
- awesome-react-hooks-cn - `react-optimistic-ui-hook`
- awesome-react-hooks - `react-optimistic-ui-hook`
- awesome-react-hooks - `react-optimistic-ui-hook`
README
![NPM](https://img.shields.io/npm/l/react-optimistic-ui-hook) [![Build Status](https://travis-ci.com/mamal72/react-optimistic-ui-hook.svg?branch=master)](https://travis-ci.com/mamal72/react-optimistic-ui-hook) [![codecov](https://codecov.io/gh/mamal72/react-optimistic-ui-hook/branch/master/graph/badge.svg)](https://codecov.io/gh/mamal72/react-optimistic-ui-hook) ![David](https://img.shields.io/david/mamal72/react-optimistic-ui-hook) ![npm](https://img.shields.io/npm/v/react-optimistic-ui-hook) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/react-optimistic-ui-hook) ![npm](https://img.shields.io/npm/dm/react-optimistic-ui-hook)
# react-optimistic-ui-hook
Minimal zero dependency "optimistic UI" pattern implementation in a React Hook.
## What is "Optimistic UI"?
> Optimistic UIs don’t wait for an operation to finish to update to the final state. They immediately switch to the final state, showing fake data for the time while the real operation is still in-progress. [Read More Here](https://uxplanet.org/optimistic-1000-34d9eefe4c05)
Note that you can search for "optimistic UI" and read more about this pattern and how it works. It simply lets your app looks faster by expecting a successful call to something like an API before getting the real response and update the interface based on that expectation.
## Installation
Using NPM:
```bash
npm install react-optimistic-ui-hook
```Using Yarn:
```bash
yarn add react-optimistic-ui-hook
```## Usage
```tsx
import React from 'react'
import { useOptimisticUI } from 'react-optimistic-ui-hook'const USERNAME = 'mamal72'
const PREDICTED_AVATAR_URL = 'https://avatars0.githubusercontent.com/u/810438?v=4'
const DELAY_IN_MS = 2000async function getGithubAvatarURL(username: string): Promise {
const response = await fetch(`https://api.github.com/users/${username}`)
const data = await response.json()return new Promise((resolve) => {
setTimeout(() => {
resolve(data.avatar_url)
}, DELAY_IN_MS);
})
}export function GithubAvatar() {
const { status, result, error } = useOptimisticUI(() => getGithubAvatarURL(USERNAME), PREDICTED_AVATAR_URL)if (status === 'failed') {
// The "result" will be the predicted image url passed to the Hook,
// But "error" is set to the raised error in the passed promise
console.error("Failed to fetch image!", error)
}if (status === 'loading') {
// The "result" will be the predicted image passed to the Hook again!
console.log('Loading image!')
}if (status === 'succeed') {
// The "result" will be the resolved promise response here!
console.log("Resolved image!", result)
}return (
Status: {status}
)
}
```## Contribution
You can report bugs and issues [here](https://github.com/mamal72/react-optimistic-ui-hook/issues/new).
You can also send a PR if you feel like you can improve or fix something. Don't forget to write/update tests for your changes.