Skip to content

Commit

Permalink
docs(renderer): replace useRef with useMemo (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
kombucha authored Feb 8, 2021
1 parent 1bebae5 commit 1d53bb9
Showing 1 changed file with 38 additions and 36 deletions.
74 changes: 38 additions & 36 deletions packages/website/docs/creating-a-renderer.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,44 +34,46 @@ const searchClient = algoliasearch(
function Autocomplete() {
// (1) Create a React state.
const [autocompleteState, setAutocompleteState] = React.useState({});
const autocomplete = React.useRef(
createAutocomplete({
onStateChange({ state }) {
// (2) Synchronize the Autocomplete state with the React state.
setAutocompleteState(state);
},
getSources() {
return [
// (3) Use an Algolia index source.
{
sourceId: 'querySuggestionsSource',
getItemInputValue({ item }) {
return item.query;
},
getItems({ query }) {
return getAlgoliaHits({
searchClient,
queries: [
{
indexName: 'instant_search',
query,
params: {
hitsPerPage: 4,
highlightPreTag: '<mark>',
highlightPostTag: '</mark>',
const autocomplete = React.useMemo(
() =>
createAutocomplete({
onStateChange({ state }) {
// (2) Synchronize the Autocomplete state with the React state.
setAutocompleteState(state);
},
getSources() {
return [
// (3) Use an Algolia index source.
{
sourceId: 'products',
getItemInputValue({ item }) {
return item.query;
},
getItems({ query }) {
return getAlgoliaHits({
searchClient,
queries: [
{
indexName: 'instant_search',
query,
params: {
hitsPerPage: 4,
highlightPreTag: '<mark>',
highlightPostTag: '</mark>',
},
},
},
],
});
},
getItemUrl({ item }) {
return item.url;
],
});
},
getItemUrl({ item }) {
return item.url;
},
},
},
];
},
})
).current;
];
},
}),
[]
);

// ...
}
Expand Down

0 comments on commit 1d53bb9

Please sign in to comment.