Force refresh of react-instantsearch resulst

Hi! I’m using react-instantsearch with react-router-4 url syncing of search state. I’d like to provide a button on my UI that when clicked triggers a refresh of algolia search results (of the currently selected parameters.)

My first naive attempt was just to update an unrelated piece of component state with a timestamp hoping that would trigger a complete re-render of the component (and thus run the search again) to no luck.

Is there a recommended way to force a re-search without changing parameters / query?

We expose a .clearCache() method on the JavaScript client. To be able to call that one with React InstantSearch, we need to have access to the JavaScript client, which by default is implicitly used, instead of explicitly. To override that, we can import the client ourselves, pass it through and then call the clearCache() method when needed. Finally you’ll get with a structure like this:

import algoliasearch from 'algoliasearch';
var algoliaClient = algoliasearch(...);
const App = () => <InstantSearch algoliaClient={algoliaClient} />;
// pass the client down to where it's used
// on the button click: 
// algoliaClient.clearCache();

more reading

Hi,

is it also possible to rerun the search after clearing cache this way? Is algoliasearch-helper needed for that?

Hey @genadij.bojev,

This is currently a bit complicated, but you can refresh it completely by unmounting and mounting the InstantSearch component according to some tests I did. We’re planning to make this easier in the future (obviously not making promises, just giving ideas).

Read more algolia/react-instantsearch#100 and algolia/react-instantsearch#252 for the proposal

Update: since version 4.3.0 you can use the refresh property on React InstantSearch: https://www.algolia.com/doc/api-reference/widgets/instantsearch/react/#widget-param-refresh

Guide available here: https://www.algolia.com/doc/guides/building-search-ui/going-further/improve-performance/react/#refresh-the-cache-triggered-by-an-action-from-the-user