-
Notifications
You must be signed in to change notification settings - Fork 386
Autocomplete, empty query and slow network. #465
Comments
@mthuret Also facing this. It seems that the workaround in the codesandbox would work only for initial results but not on change of query. Is it possible to either empty the hits (say, if optedIn) or have a status like |
The proposed workaround doesn't work depending on how you trigger the Context:
On the first example, everything works. On the second & third example you can observe a blink on the list when the input value pass from empty to something. If you have trouble to see the blink you can emulate a slower connection. Explaination:
|
@Haroenv So, I think #544 only covers providing a @bobylito can confirm? I think ideally the |
Hi folks - just following up to see if there is any update here or thoughts on what a workaround or solution might be? At the moment, it is the biggest missing element for a smooth experience on our app. Thanks for all of your work on the library! |
Hi @oyeanuj, sorry for the late answer. You are right the IMO we don't need to pass additional props to Here is a quick example that hide the suggestions when a search is in progress: const AutoComplete = ({ currentRefinement, hits, searching, refine }) => (
<div>
<input
value={currentRefinement}
onChange={event => refine(event.currentTargert.value)}
placeholder="Type a product"
/>
<div hidden={searching}>
{currentRefinement && (
<ul>{hits.map(hit => <li key={hit.objectID}>{hit.name}</li>)}</ul>
)}
</div>
</div>
);
const AutoCompleteConnected = connectAutoComplete(connectStateResults(AutoComplete)); |
@samouss I'm finding the above method works, but causes additional flickering due to |
We found another solution without the flickering effect. At the end what we want to display is only the results that match the Here is an example on CodeSandbox with & without the condition, it's easier to see the difference with a network throttling. On the first example the previous results is immediately displayed when we start typing. But on the second one the results appear only when the Let me know if it works for your use case. class AutoComplete extends Component {
state = {
hits: this.props.hits
};
componentWillReceiveProps(nextProps) {
const { currentRefinement: nextRefinement, hits, searchResults } = nextProps;
const { currentRefinement } = this.props;
const hasMatchingResults =
!!searchResults &&
!!searchResults.query &&
currentRefinement.startsWith(searchResults.query);
this.setState({
hits: hasMatchingResults && !!nextRefinement ? hits : []
});
}
render() {
const { currentRefinement, refine } = this.props;
const { hits } = this.state;
return (
<div>
<input
value={currentRefinement}
onChange={event => refine(event.currentTarget.value)}
placeholder="Type a product"
/>
<ul>
{hits.map(hit => (
<li key={hit.objectID}>
<Highlight attributeName="name" hit={hit} />
</li>
))}
</ul>
</div>
);
}
}
const AutoCompleteConnected = connectAutoComplete(connectStateResults(AutoComplete)); |
@samouss Two question here: Do you think the Also, any other thoughts on a solution for this problem? |
This should be fixed now. Also today, we recommend using the Autocomplete library instead of |
Context:
You have an autocomplete using the
connectAutoComplete
connector.On 3, the result of the query "a" or the result of empty query depending of the configuration of your autocomplete will appear first, and then you'll have b. That's producing a flash, and more importantly can be disturbing with slow network.
The reason behind this is that we do not reset hits to [] each time we perform a query, therefore when doing a new one, you first have the results of the previous query inside the props. This become a problem in case you hide some of the results like here if there's no query entered by the user.
See:
https://discourse.algolia.com/t/react-instantsearch-w-autosuggest-unwanted-initial-hit-results-displaying/2495
https://discourse.algolia.com/t/react-instantsearch-v4-1-3-is-out-tada/3167/4
Goal:
You can make some condition yourself to avoid this behaviour like I did here https://codesandbox.io/s/k9p6y7zllr taking advantage of the
componentWillReceiveProps
lifecycle method. At the bare minimum we should add this in the autocomplete guide.I'm also wondering if we could do something inside the library that can prevent this.
What do you think?
The text was updated successfully, but these errors were encountered: