0

I have two instances of AsyncTypeahead being rendered in a component by a separate class.

In order to keep the blur function tied to the right field I'm using .getInstance().

So in my constructor I'm using:

   this.typeahead = React.createRef();

Then my props for typahead are as follows:

 <AsyncTypeahead
    id="field1"
    allowNew={false}
    defaultSelected={defaultSelected}
    disabled={false}
    filterBy={(option, props) =>  true}
    inputProps={{
    id: id,
    tabIndex: -1
    }}
    isInvalid={isInError}
    isLoading={isLoading}
    useCache={false}
    labelKey="value"
    minLength={2}
    multiple={multiple}
    onBlur={this.onBlur}
    onChange={onChange}
    onFocus={onFocus}
    onSearch={this.handleSearch}
    options={options}
    placeholder="Field 1 *"
    ref={this.typeahead}
    required={required}
    value={value}
   />


 <AsyncTypeahead
    id="field2"
    allowNew={false}
    defaultSelected={defaultSelected}
    disabled={false}
    filterBy={(option, props) =>  true}
    inputProps={{
    id: id,
    tabIndex: -1
    }}
    isInvalid={isInError}
    isLoading={isLoading}
    useCache={false}
    labelKey="value"
    minLength={2}
    multiple={multiple}
    onBlur={this.onBlur}
    onChange={onChange}
    onFocus={onFocus}
    onSearch={this.handleSearch}
    options={options}
    placeholder="Field 2 *"
    ref={this.typeahead}
    required={required}
    value={value}
   />

My onBlur function is as follows:

 onBlur = () => {
    const instance = this.typeahead.getInstance();
 }

When I tab out/blur my input fields, I get the following console error:

TypeError: n.typeahead.getInstance is not a function

I can use the following (this.typeahead.current) successfully, but then my options from their dropdowns get mixed up (there are two AsyncTypeaheads on the page).

const instance = this.typeahead.current;

Using current does seem to refer to the proper DOM element, in my react dev tools, but the options that show in the dropdown are whatever was blurred last.

So if I choose a selection from field A, then choose a selection from field B, once I click back on field A it shows me the options from field B in the auto-populate.

I can't think of what else to try.

Thanks

Best Dev Tutorials
  • 452
  • 2
  • 6
  • 22
  • `getInstance` was deprecated in v4.2 and removed in v5.0. Can you please post the code for the two typeaheads? – ericgio Mar 30 '21 at 04:16
  • @ericgio I updated my question with the two typeaheads, and I also recgonize your name from GitHub on this topic. – Best Dev Tutorials Mar 30 '21 at 10:36
  • You're using the same ref for both typeaheads, so the second one is overwriting the first. You need two separate refs. – ericgio Mar 31 '21 at 04:18

0 Answers0