All Projects → awmleer → use-async-memo

awmleer / use-async-memo

Licence: MIT license
React hook for generating async memoized data.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to use-async-memo

use-change
The most minimalistic React state management library on the market with zero dependencies and React.useState-like syntax
Stars: ✭ 74 (-43.08%)
Mutual labels:  hook
crypto-watchdog
Crypto Watchdog is an open-source developer friendly project, periodically queries crypto market and notifies potential pumps & recently added tokens/coins via web-hooks.
Stars: ✭ 22 (-83.08%)
Mutual labels:  hook
logrus-redis-hook
Hook for Logrus which enables logging to RELK stack (Redis, Elasticsearch, Logstash and Kibana)
Stars: ✭ 33 (-74.62%)
Mutual labels:  hook
react-imageViewer
React component for image displaying in full screen
Stars: ✭ 61 (-53.08%)
Mutual labels:  hook
objc-format-check
🛠A code-format tool for C or OC
Stars: ✭ 75 (-42.31%)
Mutual labels:  hook
JVMTI Demo
android p jvmti/android studio apply change
Stars: ✭ 46 (-64.62%)
Mutual labels:  hook
ProcessInjector.NET
Learning Process Injection and Hollowing techniques
Stars: ✭ 23 (-82.31%)
Mutual labels:  hook
use-double-tap
React hook for handling double tap on mobile devices
Stars: ✭ 18 (-86.15%)
Mutual labels:  hook
AndroidGotHook
GOT Hook implemented in Android
Stars: ✭ 63 (-51.54%)
Mutual labels:  hook
SandVXposed
Xposed environment without root (OS 5.0 - 10.0)
Stars: ✭ 832 (+540%)
Mutual labels:  hook
use-double-click
🐁 A simple React hook for differentiating single and double clicks on the same component.
Stars: ✭ 47 (-63.85%)
Mutual labels:  hook
use-antd-resizable-header
antd表格头拖拽hook
Stars: ✭ 63 (-51.54%)
Mutual labels:  hook
SSDTHOOK
An SSDT hook for Windows
Stars: ✭ 44 (-66.15%)
Mutual labels:  hook
react-ui-hooks
🧩Simple repository of React hooks for building UI components
Stars: ✭ 20 (-84.62%)
Mutual labels:  hook
react-use-hotkeys
React hook for creating simple keyboard shortcuts
Stars: ✭ 74 (-43.08%)
Mutual labels:  hook
isMounted
React hook to check if the component is still mounted
Stars: ✭ 93 (-28.46%)
Mutual labels:  hook
use-smooth-scroll
React hook which gives a smooth scrolling function.
Stars: ✭ 41 (-68.46%)
Mutual labels:  hook
semantic-commit-hook
Git hook that enforces semantic commit messages.
Stars: ✭ 34 (-73.85%)
Mutual labels:  hook
react-cool-virtual
😎 ♻️ A tiny React hook for rendering large datasets like a breeze.
Stars: ✭ 1,031 (+693.08%)
Mutual labels:  hook
RedditVanced
Reddit Android app mod inspired by Aliucord
Stars: ✭ 41 (-68.46%)
Mutual labels:  hook

useAsyncMemo

React hook for generating async memoized data.

API

function useAsyncMemo<T>(factory: () => Promise<T>, deps: DependencyList, initial?: T): T

If factory returns undefined or null, useAsyncMemo will leave the memoized value unchanged.

Demo

Import

import {useAsyncMemo} from "use-async-memo"

Fetch API:

const data = useAsyncMemo(doAPIRequest, [])

or

const data = useAsyncMemo(() => doAPIRequest(), [])

or

const data = useAsyncMemo(() => {
  return doAPIRequest()
}, [])

or

const data = useAsyncMemo(async () => {
  return await doAPIRequest()
}, [])

Search on inputting:

const [input, setInput] = useState()
const users = useAsyncMemo(async () => {
  if (input === '') return []
  return await apiService.searchUsers(input)
}, [input], [])

Get loading status:

const [loading, setLoading] = useState(true)
const data = useAsyncMemo(async () => {
  setLoading(true)
  const response = await doAPIRequest()
  setLoading(false)
  return response
}, [])

With ability of manual clearing:

const [input, setInput] = useState()

const [clearFlag, setClearFlag] = useState({
  val: false
})
function clearItems() {
  setClearFlag({
    val: true
  })
}

const users = useAsyncMemo(async () => {
  if (clearFlag.val) {
    clearFlag.val = false
    return null
  }
  if (input === '') return []
  return await apiService.searchUsers(input)
}, [input, clearFlag], [])

With debounced value:

see use-debounce

const [input, setInput] = useState()
const [debouncedInput] = useDebounce(input, 300)
const users = useAsyncMemo(async () => {
  if (debouncedInput === '') return []
  return await apiService.searchUsers(debouncedInput)
}, [debouncedInput], [])
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].