Anyway to clear client cache using hooks? #5315
-
I've been updating our application to use I'm specifically referring to this: And it doesn't appear to exist here: Any tips would be appreciated, thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 0 comments 6 replies
-
Hey @cjbland – we've worked on the last Hook to support caching and other top-level APIs. It looks like this: function Search() {
const { refresh } = useInstantSearch();
return (
<div>
<button
onClick={() => {
refresh();
}}
>
Refresh
</button>
</div>
);
}
function App(props) {
return (
<InstantSearch {...props}>
<Search />
</InstantSearch>
);
} Here's a sandbox that implements See previous solutionFor now, you can use function connectSearchCache(renderFn, unmountFn = () => {}) {
return (widgetParams) => {
return {
$$type: 'custom.cache',
render(renderOptions) {
const { instantSearchInstance } = renderOptions;
renderFn(
{
...this.getWidgetRenderState(renderOptions),
instantSearchInstance,
},
false
);
},
getWidgetRenderState({ instantSearchInstance }) {
return {
refresh: instantSearchInstance.refresh.bind(instantSearchInstance),
widgetParams,
};
},
dispose() {
unmountFn();
},
};
};
}
function useSearchCache() {
return useConnector(connectSearchCache);
}
function Search() {
const { refresh } = useSearchCache();
return (
<div>
<SearchBox />
<button
onClick={() => {
refresh();
}}
>
Refresh
</button>
<Hits hitComponent={Hit} />
</div>
);
}
export function App(props) {
return (
<InstantSearch {...props}>
<Search />
</InstantSearch>
);
} Here's a sandbox that implements |
Beta Was this translation helpful? Give feedback.
Hey @cjbland – we've worked on the last Hook to support caching and other top-level APIs. It looks like this:
Here's a sandbox that implements
useSearchCache()
.See previous solution
For now, you can use
useConnector()
to retrieve the underlying InstantSearch.js API and expose it in React.