import React, { Component } from 'react'; import { storiesOf } from '@storybook/react'; import algoliasearch from 'algoliasearch/lite'; import { InstantSearch, SearchBox, Configure } from 'react-instantsearch-dom'; import { CustomHits, Content } from './util'; const stories = storiesOf('RefreshCache', module); const searchClient = algoliasearch( 'latency', '6be0576ff61c053d5f9a3225e2a90f76' ); class AppWithRefresh extends Component { state = { refresh: false, }; refresh = () => { this.setState({ refresh: true }, () => { this.setState({ refresh: false }); }); }; render() { return (

Feature: Refresh cache

Adding the refresh prop to your InstantSearch component gives you the possibility to refresh the cache.

How to test it?

By default, the 'refresh' prop is disabled. You will need to open your network tab in the developer tools.
  1. Type a query in the SearchBox (for instance 'cellphones'). You should see 10 requests to Algolia (one per letter)
  2. Type 'cellphones' again, you will see that no additional query is made since the results are retrieved from the cache
  3. Make sure the SearchBox is empty, click on the 'Refresh cache' button (you should see that Refresh set to true in the info box below the SearchBox)
  4. Type your previous query again: the cache has been cleared and you will see new requests made to Algolia

); } } stories.add('with a refresh button', () => ( )); class App extends Component { state = { refresh: false, count: 0, }; componentDidMount() { this.interval = setInterval( () => this.setState( (prevState) => ({ refresh: prevState.count === 5, count: prevState.count === 5 ? 0 : prevState.count + 1, }), () => { this.setState({ refresh: false, }); } ), 1000 ); } componentWillUnmount() { clearInterval(this.interval); } render() { return ( The cache is refreshed every 5 seconds. ); } } stories.add('with setInterval', () => ( ));