react-instantsearch icon indicating copy to clipboard operation
react-instantsearch copied to clipboard

Add a way to debounce the queries

Open danhodkinson opened this issue 7 years ago • 8 comments

Bug: What is the current behavior? When we have a facet we wish to search through, it fires off a search query on every keystroke. If somebody is going to type quickly then it's a waste of query volume, especially as low pricing plans only have 75 per second.

Bug: What is the expected behavior? The ability to delay/throttle this behaviour would be really useful.

danhodkinson avatar Feb 13 '18 14:02 danhodkinson

@danhodkinson you can use a library like lodash to throttle the search query in your own code

BrentLayne avatar Aug 23 '18 17:08 BrentLayne

@BrentLayne can you provide or point to an example of this?

Definitely +1 to having this functionality out of the box though.

sampsonjoliver avatar Aug 24 '18 03:08 sampsonjoliver

Here is an example that show how to implement it with connectSearchBox.

samouss avatar Aug 24 '18 13:08 samouss

Just create your own search component with a debounce:

Reference: https://dev.to/gabe_ragland/debouncing-with-react-hooks-jci

Example:

import { useEffect, useState } from 'react'
import { connectSearchBox } from 'react-instantsearch-dom'
import useDebounce from '../components/useDebounce'

/**
 * https://www.algolia.com/doc/api-reference/widgets/search-box/react/
 * https://dev.to/gabe_ragland/debouncing-with-react-hooks-jci
 */

const SearchBox = ({ currentRefinement, isSearchStalled, refine }) => {
    const [searchTerm, setSearchTerm] = useState('')
    const debouncedSearchTerm = useDebounce(searchTerm, 200)

    useEffect(() => {
        if (debouncedSearchTerm) {
            refine(searchTerm)
        } else {
            refine('')
        }
    }, [debouncedSearchTerm])

    return (
        <form noValidate action="" role="search">
            <input
                type="search"
                onChange={e => setSearchTerm(e.target.value)}
            />
            <button onClick={() => refine('')}>Reset query</button>
            {isSearchStalled ? 'My search is stalled' : ''}
        </form>
    )
}
const SearchBoxBasic = connectSearchBox(SearchBox)

export default SearchBoxBasic

approached avatar Aug 05 '21 08:08 approached

👀

mylesthedev avatar Jun 23 '22 01:06 mylesthedev

In React InstantSearch Hooks you can debounce using queryHook, as seen in this guide: https://www.algolia.com/doc/guides/building-search-ui/going-further/improve-performance/react-hooks/#disabling-as-you-type

Haroenv avatar Jun 23 '22 08:06 Haroenv

Here is an example that show how to implement it with connectSearchBox.

I kind of can only disagree strongly that this should be managed on the end user code.

kopax-polyconseil avatar Aug 08 '22 08:08 kopax-polyconseil

It's crazy to me that this is not the default or at the very least an option. I got here after thinking I missed an option or there was a bug, because it wasn't even a consideration in my mind that there wouldn't be a debounce.

And the fact that plans are based on searches means the only logical reason I can think of for there not being one is to make more money. I just typed "bullshit" into search and it made 8 requests.

robclancy avatar Aug 16 '22 08:08 robclancy

Hi, I'm closing this issue as multiple solutions are provided and there are no short term plans to have this behavior in the library.

dhayab avatar Dec 21 '22 14:12 dhayab

Hi, I'm closing this issue as multiple solutions are provided and there are no short term plans to have this behavior in the library.

I believe you should keep reopen if someone wish to pull, and because these multiple solutions are all workaround, not state of art development.

kopax-polyconseil avatar Dec 21 '22 14:12 kopax-polyconseil

Reopen this wtf

robclancy avatar Dec 21 '22 14:12 robclancy

We will not implement major new features in React InstantSearch, non-hooks, but React InstantSearch hooks, as linked earlier already supports queryHook to debounce querying

Haroenv avatar Dec 21 '22 14:12 Haroenv