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.
#
UsageContext 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.
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.
#
ReferenceThe setContext
function is accessible on your autocomplete
instance.
It's also provided in:
The context
object is available on the state
object.
setContext
#
(value: Record<string, unknown>) => void
The function to pass data to to store it in the context.
context
#
Record<string, unknown>
The context to read data from.