When root indexName is absent, `scopedResults` are empty
marcelgerber opened this issue Β· comments
π Current behavior
If I create a <InstantSearch indexName={undefined}>
element (as enabled by #5590 a year ago), with several <Index>
components inside it, and then want to access useInstantSearch()
's scopedResults
element, I find that it will always be (essentially) empty.
π Steps to reproduce
See the repro link below; click the checkbox at the very top and see how the available indexId
s change.
Live reproduction
https://codesandbox.io/p/sandbox/algolia-no-root-index-scopedresults-w2jfvl?file=%2Fsrc%2FApp.tsx
π Expected behavior
I should find entries in scopedResults
for all sub-indexes, as is the case if a root index name is given.
Package version
Algolia for JavaScript (4.23.3); Browser (lite); instantsearch.js (4.66.1); react (18.1.0); react-instantsearch (7.7.1); react-instantsearch-core (7.7.1); JS Helper (3.17.0)
Operating system
No response
Browser
No response
Code of Conduct
- I agree to follow this project's Code of Conduct
So, I could trace this down to these lines just now:
In the above case, results
is always null
(makes sense, I guess), but searchIndex.getScopedResults()
is not. But because of this if-statement, it is never updated.
That's a good point, although I'm not sure how to change this condition without then also making people deal with null
for the initial render (which people usually don't have to care about, and otherwise destructures badly). An option could be a way to go, but isn't that nice. Do you have a solution in mind?
Hm, I don't know about the internals of InstantSearch and when results are arriving, but can we detect that there is no main index present?
In that case, results
will always be null
(and has always been), and it makes sense to update scopedResults
directly.
Yes, that's a good point, so in that case the condition could be:
if (results !== null) {
setSearchResults({
results,
scopedResults: searchIndex.getScopedResults(),
});
} else if (search.mainIndex.getIndexName() === undefined) {
setSearchResults({
results: getIndexSearchResults(searchIndex),
scopedResults: searchIndex.getScopedResults(),
});
}
If you're interested in seeing this through, we'd be happy to accept a pull request with this feature and new tests :)
Hi, we just released react-instantsearch@7.7.3 and react-instantsearch-core@7.7.3 which include a fix for this issue.
Thank you @dhayab, works perfectly and flawlessly!