# Using React and Instant Search

The quickest way to get your Menu up and running on a React environment is using Algolia's [InstantSearch](https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/react/). InstantSearch is a React library that encapsulates the complexities of connecting with Algolia's indexes and helps with things like searching and filtering.

#### Optional: Adding Algolia's CSS files

If you need a basic starter CSS, add the following line into the `<head>` tag:

To have a default CSS for your project (if you don't have one yet), add the following line into the `<head>` tag:

```html
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/instantsearch.css@7.3.1/themes/reset-min.css" integrity="sha256-t2ATOGCtAIZNnzER679jwcFcKYfLlw01gli6F6oszk8=" crossorigin="anonymous">
```

Alternatively, Algolia has a theme (called "satellite") available if you prefer:

```html
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/instantsearch.css@7.4.5/themes/satellite-min.css" integrity="sha256-TehzF/2QvNKhGQrrNpoOb2Ck4iGZ1J/DI4pkd2oUsBc=" crossorigin="anonymous">
```

### Setup

To get started you need to use the [`<InstantSearch />`](https://www.algolia.com/doc/api-reference/widgets/instantsearch/react/) component. As a bare minimum, it requires an Algolia search client and the name of the index you want to search on, such as `menu-products-demo`. Make sure you ask us to provide you the applicationID and the searchKey for your application.

Here is how to set this up:

```jsx
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch } from 'react-instantsearch-dom';

const searchClient = algoliasearch(
  'YourApplicationID',
  'YourSearchOnlyAPIKey'
);

const App = () => (
  <InstantSearch
    indexName="menu-products-demo"
    searchClient={searchClient}
  >
    {/* Components here will interact with the search state */}
  </InstantSearch>
);
```

Nota that only one indexName can be used at a time.

#### Building the search and display capabilities

Next step is to add the [`<SearchBox />`](https://www.algolia.com/doc/api-reference/widgets/search-box/react/) which is the input field for the search. By default, SearchBox does not need any props but there are few interesting ones such as `autofocus` and `searchAsYouType`. For more information, see [here](https://www.algolia.com/doc/api-reference/widgets/search-box/react/).

Building from the previous snippet:

```jsx
import { InstantSearch, SearchBox } from 'react-instantsearch-dom';
{...}

const App = () => (
  <InstantSearch
    indexName="menu-products-demo"
    searchClient={searchClient}
  >
    <SearchBox 
      autoFocus
      searchAsYouType
    />
  </InstantSearch>
);
```

The following step is how to display the data that we get from the search. To display the results Algolia provides the `<Hits />` component. This will iterate over all the results and render a component you define and pass as a parameter.&#x20;

`<Hits />` will pass a parameter called `hit` which holds the entity you searched for. In this example, each `hit` is a `menu-product`.

```jsx
const ProductDisplay = ({hit}) => {(
  <>
    <b>Name:</b>
    <br />
    <div>{hit.name}</div>
    <br />
    <b>Brand:</b>
    <br />
    <div>{hit.brand}</div>
  </>
)}

<InstantSearch
  indexName="menu-products-demo"
  searchClient={searchClient}
>
  <SearchBox 
    autoFocus
    searchAsYouType
  />
  <Hits hitComponent={ProductDisplay} />
</InstantSearch>
```

Here is an example from our demo sandbox of what a menu product looks like:

<details>

<summary><strong>Expand</strong></summary>

```json
{
  "max_cart_quantity": 5,
  "allow_multiple_flower_count": false,
  "store_notes": null,
  "_geoloc": {
    "lat": 36.9643097,
    "lng": -122.0186662
  },
  "aggregate_rating": 0,
  "allergens": [],
  "amount": "",
  "at_visible_store": true,
  "available_weights": [
    "half gram",
    "gram"
  ],
  "brand_subtype": "Rosin",
  "brand": "Alpha & Omega Medz",
  "bucket_price": 25,
  "business_licenses": [],
  "cannabinoids": [],
  "category": null,
  "description": "A cannabis rosin by Alpha & Omega Medz",
  "dosage": "",
  "effects": [],
  "flavors": [],
  "image_urls": [],
  "ingredients": [],
  "kind_subtype": "Rosin",
  "kind": "extract",
  "custom_product_type": "extract",
  "name": "Deluxe OG",
  "percent_cbd": 0,
  "percent_cbda": 0,
  "percent_thc": 0,
  "percent_thca": 0,
  "percent_tac": 0,
  "photos": [],
  "has_photos": false,
  "has_thc_potency": false,
  "product_id": 1902,
  "product_percent_cbd": 0,
  "product_percent_thc": 0,
  "product_photos": [],
  "review_count": 0,
  "root_subtype": "Rosins",
  "custom_product_subtype": "Rosins",
  "sort_price": 50,
  "special_amount": null,
  "special_id": null,
  "special_title": null,
  "store_specific_product": false,
  "store_types": [
    "recreational"
  ],
  "terpenes": [],
  "type": "extract",
  "url_slug": "alpha-omega-medz-deluxe-og",
  "visible": true,
  "inventory_potencies": [],
  "operator_store_rank": null,
  "photo_weights": [],
  "feelings": [],
  "activities": [],
  "root_types": [
    "extract",
    [
      "New Price Drops-3"
    ],
    "extract:Rosins"
  ],
  "available_for_delivery": false,
  "available_for_pickup": true,
  "price_gram": "50.0",
  "discounted_price_gram": null,
  "price_two_gram": null,
  "discounted_price_two_gram": null,
  "price_eighth_ounce": null,
  "discounted_price_eighth_ounce": null,
  "price_quarter_ounce": null,
  "discounted_price_quarter_ounce": null,
  "price_half_ounce": null,
  "discounted_price_half_ounce": null,
  "price_ounce": null,
  "discounted_price_ounce": null,
  "price_half_gram": "25.0",
  "discounted_price_half_gram": null,
  "price_each": null,
  "discounted_price_each": null,
  "applicable_special_ids": [],
  "objectID": "401",
  "_snippetResult": {
    "store_id": {
      "value": "3",
      "matchLevel": "none"
    },
    "max_cart_quantity": {
      "value": "5",
      "matchLevel": "none"
    },
    "_geoloc": {
      "lat": {
        "value": "36.9643097",
        "matchLevel": "none"
      },
      "lng": {
        "value": "-122.0186662",
        "matchLevel": "none"
      }
    },
    "aggregate_rating": {
      "value": "0.0",
      "matchLevel": "none"
    },
    "amount": {
      "value": "",
      "matchLevel": "none"
    },
    "available_weights": [
      {
        "value": "half gram",
        "matchLevel": "none"
      },
      {
        "value": "gram",
        "matchLevel": "none"
      }
    ],
    "brand_subtype": {
      "value": "Rosin",
      "matchLevel": "none"
    },
    "brand": {
      "value": "Alpha & Omega Medz",
      "matchLevel": "none"
    },
    "bucket_price": {
      "value": "25",
      "matchLevel": "none"
    },
    "description": {
      "value": "A cannabis rosin by Alpha & Omega Medz",
      "matchLevel": "none"
    },
    "dosage": {
      "value": "",
      "matchLevel": "none"
    },
    "kind_subtype": {
      "value": "Rosin",
      "matchLevel": "none"
    },
    "kind": {
      "value": "extract",
      "matchLevel": "none"
    },
    "custom_product_type": {
      "value": "extract",
      "matchLevel": "none"
    },
    "name": {
      "value": "Deluxe OG",
      "matchLevel": "none"
    },
    "percent_cbd": {
      "value": "0.0",
      "matchLevel": "none"
    },
    "percent_cbda": {
      "value": "0.0",
      "matchLevel": "none"
    },
    "percent_thc": {
      "value": "0.0",
      "matchLevel": "none"
    },
    "percent_thca": {
      "value": "0.0",
      "matchLevel": "none"
    },
    "percent_tac": {
      "value": "0.0",
      "matchLevel": "none"
    },
    "product_id": {
      "value": "1902",
      "matchLevel": "none"
    },
    "product_percent_cbd": {
      "value": "0.0",
      "matchLevel": "none"
    },
    "product_percent_thc": {
      "value": "0.0",
      "matchLevel": "none"
    },
    "review_count": {
      "value": "0",
      "matchLevel": "none"
    },
    "root_subtype": {
      "value": "Rosins",
      "matchLevel": "none"
    },
    "custom_product_subtype": {
      "value": "Rosins",
      "matchLevel": "none"
    },
    "sort_price": {
      "value": "50.0",
      "matchLevel": "none"
    },
    "store_types": [
      {
        "value": "recreational",
        "matchLevel": "none"
      }
    ],
    "type": {
      "value": "extract",
      "matchLevel": "none"
    },
    "url_slug": {
      "value": "alpha-omega-medz-deluxe-og",
      "matchLevel": "none"
    },
    "root_types": [
      {
        "value": "extract",
        "matchLevel": "none"
      },
      [
        {
          "value": "New Price Drops-3",
          "matchLevel": "none"
        }
      ],
      {
        "value": "extract:Rosins",
        "matchLevel": "none"
      }
    ],
    "price_gram": {
      "value": "50.0",
      "matchLevel": "none"
    },
    "price_half_gram": {
      "value": "25.0",
      "matchLevel": "none"
    }
  },
  "_highlightResult": {
    "brand_subtype": {
      "value": "Rosin",
      "matchLevel": "none",
      "matchedWords": []
    },
    "brand": {
      "value": "Alpha & Omega Medz",
      "matchLevel": "none",
      "matchedWords": []
    },
    "description": {
      "value": "A cannabis rosin by Alpha & Omega Medz",
      "matchLevel": "none",
      "matchedWords": []
    },
    "name": {
      "value": "Deluxe OG",
      "matchLevel": "none",
      "matchedWords": []
    },
    "root_types": [
      {
        "value": "extract",
        "matchLevel": "none",
        "matchedWords": []
      },
      [
        {
          "value": "New Price Drops-3",
          "matchLevel": "none",
          "matchedWords": []
        }
      ],
      {
        "value": "extract:Rosins",
        "matchLevel": "none",
        "matchedWords": []
      }
    ]
  },
  "_rankingInfo": {
    "nbTypos": 0,
    "firstMatchedWord": 0,
    "proximityDistance": 0,
    "userScore": 249,
    "geoDistance": 0,
    "geoPrecision": 1,
    "nbExactWords": 0,
    "words": 0,
    "filters": 0
  }
}
```

</details>

All together:

```jsx
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch, SearchBox, Hits } from 'react-instantsearch-dom';

const searchClient = algoliasearch(
  'YourApplicationID',
  'YourSearchOnlyAPIKey'
);

const ProductDisplay = ({hit}) => {(
  <>
    <b>Name:</b>
    <br />
    <div>{hit.name}</div>
    <br />
    <b>Brand:</b>
    <br />
    <div>{hit.brand}</div>
  </>
)}

const App = () => {
  return (
    <InstantSearch
      indexName="menu-products-demo"
      searchClient={searchClient}
    >
      <SearchBox 
        autoFocus
        searchAsYouType
      />
      <Hits hitComponent={ProductDisplay} />
    </InstantSearch>
  )
};
```

That's it! You can now query and display menu products from Algolia with just a few components. \
\
In the next section we will go over some more options available for your Menu


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.iheartjane.com/jane-docs/implementing-roots/building-your-menu/using-react-and-instant-search.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
