Skip to content

feat(InstantSearch.js): add InfiniteHits async transfomItems #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

baileypeter
Copy link

Adding a new sample illustrating how to asynchronously load data into a hit template using transformItems in a infiniteHits page.

@Haroenv
Copy link
Contributor

Haroenv commented Feb 10, 2022

Does this actually work? We don't wait for the result of transformItems, so I don't really understand how transformItems can be async, especially not a promise as it's here.

In almost all cases you don't want transformItems to be asynchronous, but if that is the case, you likely want to transform the search result, like this:

const searchClient = algoliasearch();
const search = searchClient.search;
searchClient.search = function newSearch(queries) {
  return searchClient.search(queries).then(response => {
    // transform response as you want, possibly async
    return response;
  });
}

@tkrugg
Copy link
Contributor

tkrugg commented Feb 25, 2022

It's an interesting use case. I saw Peter being asked this a lot.
It's typical when you'd like to enrich the returned records with data from another request, before printing it. In many cases, either customers do DOM manipulation or turn to backend search.
Are there any difficulties starting to support return type Promise, at least for Hits/InfiniteHits?

@Haroenv
Copy link
Contributor

Haroenv commented Feb 28, 2022

I don't think that's the right approach. You will end up with UI flashes very easily. The correct place for asynchronous logic is after the search has happened (maybe we can make that api simpler?), but definitely not in rendering. The fact that a timeout is called after the transformItems is done is mixing imperative and declarative UI, and definitely not a pattern we should use

@baileypeter
Copy link
Author

baileypeter commented Mar 11, 2022 via email

@Haroenv
Copy link
Contributor

Haroenv commented Mar 14, 2022

There's many ways, and it depends on what you'd do exactly, but here's an example that slowly transforms an attribute to uppercase: https://codesandbox.io/s/sad-morning-4wg6mk?file=/src/app.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants