Skip to content
Discussion options

You must be logged in to vote

I think you need debounce, not dedupe. You will have to debounce the key updates before giving it to SWR, e.g. using use-debounce:

const [query, setQuery] = useState("")
const [debouncedQuery] = useDebounce(query, 200)
const { data } = useSWR(`/search?q=${debouncedQuery}`)

This isn't something that SWR itself can do here. Because the old request /search?q=f has been sent already, and when it gets updated to /search?q=fo, SWR can only start the new request ASAP. By wrapping it with useDebounce, it will wait for 200ms before returning the new query, to make sure there aren't any new changes.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by MonstraG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants