react-autosuggest icon indicating copy to clipboard operation
react-autosuggest copied to clipboard

React 18: `getSectionSuggestions` is called with `undefined` after selecting suggestion on mobile device

Open OliverJAsh opened this issue 3 years ago • 4 comments

Are you reporting a bug?

https://stackblitz.com/edit/oliverjash-react-autosuggest-clear-and-focus-qi2jdy?file=src%2FApp.js

On a mobile/touch device, select a suggestion.

Observed behaviour: Runtime exception: TypeError: Cannot read properties of undefined (reading 'languages'). It seems that getSectionSuggestions is called after onSuggestionsClearRequested with a value of undefined.

Expected behaviour: No runtime exception

https://user-images.githubusercontent.com/921609/194027452-a9bbd9e5-e809-48f5-a892-e82e623c1fa6.mov

Here's another reduced test case using exactly the same code but running with React 17 instead of React 18: https://stackblitz.com/edit/oliverjash-react-autosuggest-clear-and-focus-ajv2xi?file=src%2FApp.js. This one doesn't have the same problem. The problem only seems to occur when using React 18 and the new createRoot API.

Note also that this problem does not occur when we're not using multiSection: https://stackblitz.com/edit/oliverjash-react-autosuggest-clear-and-focus-w7yg7r?file=src%2FApp.js

OliverJAsh avatar Oct 05 '22 09:10 OliverJAsh

For anyone looking to quickly workaround this issue, we've changed our implementation of getSectionSuggestions like the following

-declare const getSectionSuggestions: (s: Section) => Array<Suggestion>
+declare const getSectionSuggestions: (s: Section | undefined) => Array<Suggestion>

Magellol avatar Oct 13 '22 15:10 Magellol

For anyone looking to quickly workaround this issue, we've changed our implementation of getSectionSuggestions like the following

-declare const getSectionSuggestions: (s: Section) => Array<Suggestion>
+declare const getSectionSuggestions: (s: Section | undefined) => Array<Suggestion>

Sorry, how do you implement this? Where do you put that line? Thanks

pav-w avatar Jul 11 '23 15:07 pav-w

@pav-w Sorry if this wasn't clear. It looks like under React 18 your getSectionSuggestions callback can now be called with undefined which I believe is a bug from this library. You now need to handle this as well. For us we return an empty array when undefined is passed.

Magellol avatar Jul 17 '23 13:07 Magellol

Yup this works!

For anyone looking to quickly workaround this issue, we've changed our implementation of getSectionSuggestions like the following

-declare const getSectionSuggestions: (s: Section) => Array<Suggestion>
+declare const getSectionSuggestions: (s: Section | undefined) => Array<Suggestion>

If you are not using TS, this is the JS-equivalent

const getSectionSuggestions = (section) => (section ? section.suggestions : []);

adriangohjw avatar Oct 16 '23 09:10 adriangohjw