Accessing data with Context

The Context lets you store data and access it in different lifecycle hooks.

Sometimes you need to store arbitrary data so you can access it later in your autocomplete. For example, when retrieving hits from Algolia, you may want to reuse the total number of retrieved hits in a template.

Autocomplete lets you store data using its Context API and access it anywhere from the state.


Context exposes a setContext function, which takes an object and merges it with the existing context. You can then access the context in state.context.

The following example stores the number of hits from an Algolia response, making it accessible everywhere in your autocomplete.

// ...
getSources({ query, setContext }) {
return getAlgoliaResults({
queries: [
indexName: 'instant_search',
}).then(([products]) => {
nbProducts: products.nbHits,
// You can now use `state.context.nbProducts`
// anywhere where you have access to `state`.
return [
// ...

Context can be handy when developing Autocomplete plugins. It avoids polluting the global namespace while still being able to pass data around across different lifecycle hooks.

function createAutocompletePlugin() {
return {
// ...
subscribe({ setContext }) {
autocompletePlugin: {
// ...


The setContext function is accessible on your autocomplete instance.

It's also provided in:

The context object is available on the state object.


(value: Record<string, unknown>) => void

The function to pass data to to store it in the context.


Record<string, unknown>

The context to read data from.