几行代码实现 suspense fetch,兼容 SSR
yarn add suspense-use
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 })
}
useSuspense(callback: () => Promise, param: Record<string, any>)
:param 作为依赖参数