若您正在 React 應用程式中使用 TanStack Pacer,我們推薦使用 React 轉接器 (React Adapter)。此轉接器在核心 Pacer 工具之上提供了一系列易用的鉤子 (hooks)。如果您想直接使用核心 Pacer 的類別/函式,React 轉接器也會重新匯出核心套件的所有內容。
npm install @tanstack/react-pacer
npm install @tanstack/react-pacer
請參閱 React 函式參考文件 以查看 React 轉接器中提供的完整鉤子清單。
從 React 轉接器匯入專為 React 設計的鉤子:
import { useDebouncedValue } from '@tanstack/react-pacer'
const [instantValue, instantValueRef] = useState(0)
const [debouncedValue, debouncer] = useDebouncedValue(instantValue, {
wait: 1000,
})
import { useDebouncedValue } from '@tanstack/react-pacer'
const [instantValue, instantValueRef] = useState(0)
const [debouncedValue, debouncer] = useDebouncedValue(instantValue, {
wait: 1000,
})
或是匯入由 React 轉接器重新匯出的核心 Pacer 類別/函式:
import { debounce, Debouncer } from '@tanstack/react-pacer' // 無需另行安裝核心套件
import { debounce, Debouncer } from '@tanstack/react-pacer' // 無需另行安裝核心套件
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.