josue
December 3, 2022, 6:02am
#1
I’m trying to refresh Algolia indices via on a React Native project but the list on React Native does not refresh the list when new objects are inserted into the Algolia index. I can see the index being updated on the Indices dashboard on Algolia website but not on mobile.
Any help is much appreciated.
The prop to “refresh” the list is “refresh={boolean}”.
Code example:
import { InstantSearch } from 'react-instantsearch-hooks'
const [refresh, setRefresh] = useState(false);
const updateList = () => {
setRefresh=(true);
}
<InstantSearch
indexName={"algolia-index"}
searchClient={searchClient}
refresh={refresh}
>
.......
<Button
onPress={updateList}
></Button>
josue
December 7, 2022, 8:14pm
#2
For anyone trying to refresh their infinite hits on a react native project using react-instantsearch-hooks, unfortunately the refresh={true} prop does not work. Even though they recommend to upgrade AND it also states that IT IS compatible with React Native.
“In React InstantSearch Hooks refresh is a function returned from useInstantSearch, but it indeed doesn’t clear the infinite hits cache either. Sorry!”
opened 01:49PM - 06 Aug 19 UTC
Library: InstantSearch.js
Library: React InstantSearch
Library: React InstantSearch Hooks
Library: Vue InstantSearch
<!--
Thanks for participating in this project! We will try to answer as soon … as possible 🙏
-->
**Describe the bug 🐛**
When updating the `refresh` token, infinite hits will not react to the refetch.
**To Reproduce 🔍**
Steps to reproduce the behavior:
1. go to the example
2. trigger next page
3. refresh
4. see no changes in the infinite hits, even if the index would have changed
The example's index can be changed to an index you control where you make changes to make the effect clearer.
> https://codesandbox.io/s/react-instantsearch-app-dq2ro
**Expected behavior 💭**
Infinite hits reacts to refresh, either by:
1. clearing the cache completely and restarting on the current page
3. clearing the cache and the state and restarting on page 0
Likely we should go for option 2, unless there's a showPrevious.
**Additional context**
Add any other context about the problem here.
When refresh happens, we need to make sure the infinite hits internal cache is also invalidated.
On React Native, where the list component itself is stateful, we can not rely on the "key" hack, because it rerenders with an empty state when we simply clear the cache. What could be an option is:
1. clear cache
2. redo current search
3. save in cache
4. rerender
The problem is that you can't do that as a user reasonably, since you don't have access to the helper state.
A possible solution is:
In the function `refresh` in InstantSearchManager, emit an event to all widgets. Then in InfiniteHits, listen to that event, and clear the internal cache as we expect.
Another potential solution is to return a promise from the `search` which happens in refresh. This should allow people to rerender the InfiniteHits component manually.
Relevant pieces of code:
https://github.com/algolia/react-instantsearch/blob/ec9e0fbd6106d1c3e47f1dbfa6eaac3e20af6bd5/packages/react-instantsearch-core/src/core/createInstantSearchManager.js#L69-L72
Relevant issues:
https://github.com/algolia/react-instantsearch/issues/2464
1 Like
josue
December 7, 2022, 8:15pm
#3
If anyone has any hacks and or workarounds, first round of beer or coffee is on me…
Thanks for reposting the issue here. If I think of a workaround I’ll come back to it, but I don’t have any ideas right now.