question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pressing Up or Down Key App crashing with undefined items

See original GitHub issue

Are you reporting a bug?

@moroshko

  • The steps to reproduce the issue, e.g.:

    1. Focus on the input field
    2. Type c, and wait for suggestions to appear
    3. Press Up or Down Key

    Observed behaviour: If i press Up the Last key is selected and vice versa

    Expected behaviour: Suggestions should be Open and it should highlight the selected one once i scroll will Up arrow key

    In my application, when user types ‘c’ it is filtering and it should open the filtered list and when user presses up arrow it should scroll highlighting the keys.

Currently i am getting the below error:

screen shot 2018-10-24 at 1 08 08 pm screen shot 2018-10-24 at 1 08 22 pm

My code is as below:

import axios from “axios”; import * as myConstClass from “…/utils/constants”; import React, { Component } from “react”; import Autosuggest from “react-autosuggest”; import getDropDownData from “…/services/dropdown_service”; import Chatroom from “…/components/Chatroom”;

class AutoCompleteDAS extends Component { constructor(props) { super(props); this.state = { value: “”, suggestions: [], placeholderTxt: “” }; this.sendValue = “”; this.showValue = “”; }

componentDidMount() { this.getDropdownList(this.props.onDropdownType); }

/**

  • function to fetch suggestions object array according to user input */ getSuggestions(value, languages) { const escapedValue = this.escapeRegexCharacters(value.trim()); if (escapedValue === “”) { return languages.filter(language => language.label); } const regex = new RegExp(“^” + escapedValue, “i”); return languages.filter(language => regex.test(language.label)); }

/**

  • function to ignore escape regex characters / escapeRegexCharacters(str) { return str.replace(/[.+?^${}()|[]\]/g, “\$&”); }

/**

  • function to get a label key of the list array */ getSuggestionValue(suggestion) { return suggestion.label; }

/**

  • function which returns JSX template of autocomplete elements which is to be shown to user as dropdown items */ renderSuggestion(suggestion) { return <span>{suggestion.label}</span>; }

/**

  • axios call to get all country list and send it to chatroom component */ getDropdownList(onDropdownType) { let apiName = “”; let apiMethod = “”; let apiBody = {}; switch (onDropdownType) { case “getCountryCode”: this.showValue = “label”; this.sendValue = “value”; this.setState({ placeholderTxt : “Select Your Country…” }); apiName = “fetch_country_codes”; apiMethod = “GET”; apiBody = {}; break; case “getRejectionCode”: this.showValue = “rejectiondescription”; this.sendValue = “rejectioncode”; this.setState({ placeholderTxt : “Select Your Rejection Code…” }); apiName = “get_rejection”; apiMethod = “POST”; apiBody = { userId: this.props.passParams.userId, region: this.props.passParams.userRegion, uniqueId: this.props.passParams.uniqueId, rejectionCode: “Y” }; break; default: break; }
getDropDownData(apiName, apiMethod, apiBody)
  .then(
    response => {
      this.languages = response.data.data || response.data;
      this.transformResponse();
      this.props.onCountryListFetch(this.languages);
    },
    err => {}
  )
  .catch(err => {});

}

transformResponse() { this.languages = this.languages.map(language => { return { label: language[this.showValue], value: language[this.sendValue] }; }); }

/**

  • Function which is called when user selects any item from dropdown ad change event gets fired */ onChange = (event, { newValue, method }) => { this.setState({ value: newValue }); };

/**

  • function which is called when user types something to autocomplete input box */ onSuggestionsFetchRequested = ({ value }) => { this.setState({ suggestions: this.getSuggestions(value, this.languages) }); };

/**

  • Function which is called when it is required to clear all suggestions */ onSuggestionsClearRequested = () => { this.setState({ suggestions: [] }); };

/**

  • Function which notify if suggestions to be rendered
  • this function indicates that even if there is no user input, suggestions needs to be rendered */ // shouldRenderSuggestions(value) { // return value.trim().length >= 0; // } render() { const { value, suggestions } = this.state; console.log((this.state.suggestions = this.state.value)); const inputProps = { placeholder: this.state.placeholderTxt, value, onChange: this.onChange, onFocus: this.onFocus }; return ( <Autosuggest suggestions={suggestions} onSuggestionsFetchRequested={this.onSuggestionsFetchRequested} onSuggestionsClearRequested={this.onSuggestionsClearRequested} getSuggestionValue={this.getSuggestionValue} renderSuggestion={this.renderSuggestion} inputProps={inputProps} /> ); } }

export default AutoCompleteDAS;

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:8

github_iconTop GitHub Comments

1reaction
ttsirkiacommented, May 11, 2019

I had the same problem and the reason was that my getSuggestionValue did not return a value for every possible suggestion. It used a field that did not exist for all items.

0reactions
khrome83commented, Feb 6, 2021

For me the issue was the some of the multi indexes could be empty. And highlightFirstSuggestion was set to true. Screen Shot 2021-02-06 at 6 24 30 AM

The solution was to filter suggestions to omit anything with a empty hits array. Once I never sent a empty array of a individual index, it worked as expected.

Read more comments on GitHub >

github_iconTop Results From Across the Web

jQuery autocomplete: undefined value pressing the UP ...
If I press the up arrow key again, an error appears and my app crashes: uncaught TypeError: Cannot read property 'value' of undefined...
Read more >
Common Flutter errors
Introduction. This page explains several frequently-encountered Flutter framework errors and gives suggestions on how to resolve them.
Read more >
Eight Practices in React That Will Crash Your App in the ...
Inside our app component, if dates end up being falsey , it will be initialized with ... But our app will crash when...
Read more >
My App Crashed, Now What? - RayWenderlich.com
Note: Exception breakpoints are triggered by things going wrong in the Objective-C runtime, which in most cases means things internal to UIKit.
Read more >
App crashing immediately on start
indicates that the app crashed because it's accessing an invalid pointer, one that doesn't point to mapped memory. And indeed that pointer looks...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found