question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[react-instantsearch-hooks] hits/hits.results from useInfiniteHits

See original GitHub issue

Howdy!

We are using useInfiniteHits to return us a nice list with results for our overview page that has a ‘load more items’ option. We are doing this all client side, and aren’t working with the SSR provider just yet

We started meddling with routing and controlling the UI state through the initialUiState prop on the InstantSearch component. Now let’s say the state we provide in the initialUiState is as follows

      initialUiState={{
        products: {
          configure: {
            hitsPerPage: 10,
          },
          menu: {
            currency: 'euro',
          },
        },
      }}

The state throughout the application that is retrieved through the hooks is reflecting this initial UI state nicely - so far all good except for this one:

  const { hits, results, showMore } = useInfiniteHits(props);

The first set of hits (the first 10, based on the configuration provided in initialUiState) seem to be any hit, so: hits that are not filtered based on the initialUiState, but simply the first 10 of the products index it’s default ordering.

Now this is where it gets a bit funky:

In the results prop, the correct 10 items are actually provided - so Algolia did actually run the query and does have the correct hits. Of course, the results.hits are only the items of the current page, so we can’t use this prop with our showMore set-up.

Once we initiate the showMore, and thus load the next 10 results (to get a total of 20 hits): the hits prop is still showing the first 10 items to be any hit. The next 10 (page 2), are the items we would expect to show up on page 2.

Once we turn on a random facet or sorting option, and turn it back again to a state that matches the initialUiState as provided, everything is correct. It seems to ‘trigger’ something that makes the output of useInifniteHits go to the correct state (just one extra rerender that fixes it?)

tl;dr: It seems to be that the first set of hits in the hits property of useInifniteHits doesn’t reflect a filtered items, based on the initialUiState provided. results.hits actually has the correct items, but since we are implementing a ‘click here to show more’ scenario, we can’t use this.

Let me know if this makes sense, if it doesn’t I’m happy to try to replicate the situation

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
RobbyUitbeijersecommented, Apr 26, 2022

Interestingly multiple issues I was facing (including the one reported) kind of ‘vanished’ once I moved our application from using multiple instances of the useInifniteHits() (note: all instances were without any options/props) hook to just one singular instance of that function high up in the component tree, and then passing on the hits and results instead of getting them from the hook everywhere.

I’m still aiming to create that repro, but I might not get to it today and wanted to let you know about this finding as it seems rather specific / interesting. Not sure what could cause the mismatch in data throughout the tree in some of the instances 🤔

[Edit] it also means it’s actually unrelated to my initial suspect the (managing state manually), something else seems to be going on

0reactions
Haroenvcommented, Apr 27, 2022

What could be interesting to see if it could be related to the nesting or multiple times calling the same hook from the same component or its children. Do you see any minimal reproduction or does this only reproduce in a more “complex” app for now?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[react-instantsearch-hooks] hits/hits.results from useInfiniteHits
The first set of hits (the first 10, based on the configuration provided in initialUiState ) seem to be any hit, so: hits...
Read more >
InfiniteHits | React InstantSearch Hooks - Algolia
Infinite hits are a list of results after which you can load more results without going to a next page. You can also...
Read more >
react-instantsearch-hooks-web - npm
React InstantSearch Hooks Web is an open-source React library that lets you create an instant search result experience using Algolia's ...
Read more >
react-instantsearch | Yarn - Package Manager
types: export correct types for react-instantsearch-hooks-web (#3454 (a863430) ... results: revert commit that ensure hits are returned only if right ...
Read more >
Inserting Middleware(insights) with Instantsearch react
const { hits, sendEvent, ... } = useInfiniteHits(props) ... InstantSearchSSRProvider } from 'react-instantsearch-hooks-web' import SearchBox ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found