Skip to content

Latest commit

 

History

History
51 lines (38 loc) · 879 Bytes

README.md

File metadata and controls

51 lines (38 loc) · 879 Bytes

useSuspense

几行代码实现 suspense fetch,兼容 SSR

Install

yarn add suspense-use

Usage

import { useSuspense } from 'suspense-use'

const resolve = () => {
  return new Promise(r => {
    setTimeout(() => {
      r('resolved')
    }, 1000)
  })
}

function App() {
  const [_, value] = useSuspense(() => resolve())
}

使用参数,resolve 中需要的参数需要传递给 useSuspense

import { useSuspense } from 'suspense-use'

const resolve = () => {
  return new Promise(r => {
    setTimeout(() => {
      r('resolved')
    }, 1000)
  })
}

function App() {
  const [name, setName] = useState('snake')
  // 每次 name 变化触发重新请求
  const [isPending, value] = useSuspense(() => resolve(), { name })
}

Api

useSuspense(callback: () => Promise, param: Record<string, any>):param 作为依赖参数