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.

Asynchronous Searching Live Example Not Working Properly

See original GitHub issue

In the Live Examples, under the section Asynchronous Searching, there is an example with the following code:

class AsyncExample extends React.Component {
  state = {
    allowNew: false,
    isLoading: false,
    multiple: false,
    options: [],
  };

  render() {
    return (
      <Fragment>
        <AsyncTypeahead
          {...this.state}
          labelKey="login"
          minLength={3}
          onSearch={this._handleSearch}
          placeholder="Search for a Github user..."
          renderMenuItemChildren={(option, props) => (
            <GithubMenuItem key={option.id} user={option} />
          )}
        />
        <FormGroup>
          {this._renderCheckboxes()}
        </FormGroup>
      </Fragment>
    );
  }

  _renderCheckboxes() {
    const checkboxes = [
      {label: 'Multi-Select', name: 'multiple'},
      {label: 'Allow custom selections', name: 'allowNew'},
    ];

    return checkboxes.map(({label, name}) => (
      <Control
        checked={this.state[name]}
        key={name}
        name={name}
        onChange={this._handleChange}
        type="checkbox">
        {label}
      </Control>
    ));
  }

  _handleChange = (e) => {
    const {checked, name} = e.target;
    this.setState({[name]: checked});
  }

  _handleSearch = (query) => {
    this.setState({isLoading: true});
    makeAndHandleRequest(query)
      .then(({options}) => {
        this.setState({
          isLoading: false,
          options,
        });
      });
  }
}`

The code above is resulting in a weird search bug (as far as I can see), which is as follows: When I search for “ale”, it finds numerous users in the dropdown, which is to be expected, next I add a character to “ale” which I know is not likely to be an existent username, for example “ale!”, it does not find a user with such username, again as expected. Although after I delete the “!” character, I’m again left with “ale” in my search, although unexpectedly I do not get the same results as I did when I first typed in “ale”, instead I got no results of users with such a username, which reminds me of my “ale!” search. It’s as if it did not recognize the character deletion from the search, and it still tried to search a username “ale!”. So I added a few print statements to debug the code, and actually saw that when I delete the “!” character, the __handleSearch function is not called at all, the last call is with the “ale!”. When I set the “isLoading” variable in the state to be always false, it actually worked… Here are the screenshots that show the issue: img1 img2 img3

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
AJRenoldcommented, Feb 8, 2019

I am working on a feature using this component and solved this issue by setting the useCache prop to false.

Hopefully this can be a useful breadcrumb to someone who further investigates this issue.

0reactions
jbialobrcommented, Feb 24, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

Asynchronous Searching & Reordering - javascript
I've been asked to build a new search to act like the hotel search results on hipmunk or kayak - where I load...
Read more >
Introducing asynchronous JavaScript - Learn web development
In this article, we'll start by looking at the problem with long-running synchronous functions, which make asynchronous programming a necessity.
Read more >
Understanding the Event Loop, Callbacks, Promises, and ...
To do this, you will first learn about the original way to ensure asynchronous code is handled correctly by the event loop: callback...
Read more >
How to Check Internet Connection Status Using Async ...
The solution will use JavaScript's Fetch API and asynchronous code with ... A quick web search for “navigator online not working” reveals ...
Read more >
What is Asynchronous and What Does it Mean? - TechTarget
Sending an email or text is an example of an asynchronous form of ... can pose problems when a proper organizational structure isn't...
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