React SDK

SearchProvider

The SearchProvider should be used as a wrapper for the entire application to provide a way to share application state between hooks, for example, the current query, active filters or the search response.

Usage

It must wrap any components where you are using hooks, usually this is done in an App or index file. You must also specify the pipeline you wish to query.

  • Replace the placeholders with your account and collection IDs from the collection crendentials section in the console.
  • Replace the pipeline name placeholder with the pipeline you'd like to use to query.

Sync URL state

Enable the syncURLState prop to synchronize the state of your search UI with the browser URL. By default, it works out of the box for syncing the state of query, filters, sort and resultsPerPage while providing a support for external params.

Editable Example
​
Suggestions
    Site search by

    Loading...

    Note

    It is recommended that you only need to use SearchProvider once at the root of your application, if you find the need to use multiple collections then having multiple SearchProviders would be okay too. You shouldn't nest SearchProviders because each provider will create a separate new context which makes components beneath it (Pagination, Sorting) not behaving as they should.

    Props

    NameTypeDefaultDescription
    searchProviderPipelineConfigA pipeline configuration for search requests.
    autocompleteProviderPipelineConfigA pipeline configuration for autocomplete and typeahead requests. This is optional, by default we'll use an autocomplete pipeline.
    searchOnLoadbooleanWhether to search on initial load. Note: if you're also calling the useSearch hook in your app, you'll make two search requests.
    initialResponsestringAn initial search response in JSON format, useful for server-side rendering.
    defaultFilterstringA default filter to apply to all search requests.
    syncURLStateboolean | SyncURLStatefalseAllow to synchronize the state of your search UI with the browser URL.

    ProviderPipelineConfig

    NameTypeDefaultDescription
    pipelinePipelineConfig which pipeline to use for search queries.
    variablesVariablesA simple key -> value pair object used for every search request. It includes critical data such as the query, results per page, current page, etc.
    configConfigDefine mapping between key/value pair params to be sent with each and every request.
    fieldsFieldDictionaryA configuration is used to map fields in your data to the required fields to display in the UI.
    filters(FilterBuilder or RangeFilterBuilder)[]A list of filters is used to navigate and find relevant results.

    SyncURLState

    NameTypeDefaultDescription
    delaynumber500The delay time to synchronize the param to URL
    replacebooleanfalseIf true, modifies the current history entry, replacing it with the new state object
    extendedParamsExtendedParam[]falseThe extend params to be synchronized with the URL query params
    paramKeysParamKeys{ q: 'q', resultsPerPage: 'show', page: 'page', sort: 'sort'}The names of the URL query params

    ExtendedParam

    NameTypeDefaultDescription
    keystringKey of the param
    callbackstringCallback when the URL param changes
    defaultValuestring | number | boolean | string[] | number[] | boolean[]Default value of the param
    valuestring | number | boolean | string[] | number[] | boolean[]Current value of the param

    ParamKeys

    NameTypeDefaultDescription
    qstring'q'The name of the search query URL param
    resultsPerPagestring'show'The name of the total results per page URL param
    pagestring'page'The name of the current page URL param
    sortstring'sort'The name of the sort URL param