Stats and RefinementList not updating when changing index

Hello !

I have two different index which have different data. I created an <Index /> and passed the indexName as a variable value. the hits are being displayed thanks to a connectStateResults. The hits and pagination are displayed correctly depending on which index i’m using, but the Stats and RefinementList components aren’t, they keep being the same whatever the current index is.

Do anyone have any idea why?

Thanks a lot :slight_smile:

Hi @thomas2,

Can you give us a code sample showing your issue?

Here’s a code sample with two indices with separate Stats and RefinementList: https://codesandbox.io/s/k32w30vqzr

Hi ! Thanks for the quick answer, really appreciate that :slight_smile:

(Sorry, could not reply before, hope you don’t mind) !

I fixed my initial issue, but the solution is dirty and should not be a permanent one (dynamically changing the indexName of the InstantSearch component :confused: ), it works, but I don’t like it at all.

I was thinking, is it possible to use multiple Index but using only one of them at a time while still being able to get informations like the number of hits for each of them in real time?
I tried a lot of things but none of them has the desired result.

Thanks a lot !

Hi @thomas2,

I’m not sure to understand the fix you’re talking about. If you could provide a code sample, that would be awesome :slight_smile:

For the second part, I’m not sure to get what you mean by “using only one of them at a time”? You could definitely have 3 stats widget, one under each Index, and then, conditionally choose to render the Hits under the one you want.

Is that what you’re trying to achieve?

Thanks for the answer :slight_smile:

Here is a sample code.

I see what you mean there, I will try it and get back to you!

Awesome.

By curiosity, why you don’t like changing dynamically the indexName of InstantSearch?

Another solution would be to wrap it under one component that a prop indexName and on the upper component conditionally decide which component to display:

const app = this.state.index ? <App index="index"/> : <App index="anotherIndex"/>
return <div>{app}</div>

Well, I keep seeing multi-index examples using the <Index /> component but none of them dynamically changing the props on the InstantSearch’s component itself, I thought it was a no-op :sweat_smile:

The only problem I have with this solution is the fact that I can’t retrieve the number of hits of multiple indexes at a the same time, I can only get one depending on which index i’m currently on.

I guess I will have to use two Stats’ components linked to their respective Index.

I know how hard it is to understand me when there is no real context, sorry about that :confused:

I just saw you were French as well! can we discuss by DMs? Thanks :slight_smile:

Edit : I successfully achieved what I wanted, I placed a parent Index for each Stats component, that way I can get the correct number of hits for each. Thanks a lot for your time and patience, I learnt a lot of things :slight_smile:

Awesome @thomas2! Happy Coding :slight_smile:

Thanks, happy coding to you too :slight_smile: