Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deleting of letters from query when typing fast #494

Open
PrincAm opened this issue Jan 9, 2018 · 4 comments
Open

Deleting of letters from query when typing fast #494

PrincAm opened this issue Jan 9, 2018 · 4 comments

Comments

@PrincAm
Copy link

PrincAm commented Jan 9, 2018

I'm heading to issue with autocomplete when I'm trying to asynchronously fetch data via onSuggestionFetchRequest. Some letters are deleted from the query when user is typing fast. This issue is proved only in IE11. Using React version 16.2.0, React Autosuggest version 9.3.2 and Redux for global state management.

First, I thought it is clearly connected to well described problem with onChange in IE11 but an update of React to version 16 didn't help.

Then, I found the problem with canceling of old async request when fetching data. Unfortunately, this did not help as well.

Any help or hint is highly appreciated!

Example of code:

const mapDispatchToProps = (dispatch, ownProps) => ({
  onSuggestionsFetchRequested_test: (latestRequest) => ({value, reason}) => {
    if (reason === 'input-changed') {
      dispatch(start(value))
      const thisRequest = latestRequest = fetch(`/autosuggest/request`)
        .then((resp) => resp.json())
        .then((json) => {
          if (thisRequest !== latestRequest) {
            return
          }
          dispatch(success(paramsHash, json))
        })
    }
  }
  
export class Component extends React.Component {
  constructor(props) {
    super(props)
    this.latestRequest = null
  }

  render() {
    const {onSuggestionsFetchRequested_test} = this.props

    const inputProps = {
      value,
      onChange,
      autoFocus: true,
      placeholder = 'Enter query...'
    }

    const onSuggestionsFetchRequested = onSuggestionsFetchRequested_test(this.latestRequest)

    const autosuggestProps = {
      suggestions,
      onSuggestionsFetchRequested,
      onSuggestionsClearRequested,
      getSuggestionValue,
      renderSuggestion,
      inputProps
    }

    return (
      <Autosuggest {...autosuggestProps} />
    )
  }
}
@PrincAm PrincAm changed the title Deleting letters from query when typing fast Deleting of letters from query when typing fast Jan 9, 2018
@ianlyons
Copy link

@moroshko I'm having the same issue, albeit on 15.6.2 -- have you had any other reports of issues around this with React 16?

@vKongv
Copy link

vKongv commented May 22, 2018

Having the same issue in IE as well. Any update?

@PrincAm
Copy link
Author

PrincAm commented May 23, 2018

In our case, this issue was caused by race condition when fetching suggestions. If user types, request is send with every key press and then autosuggest value can be updated by old response. Since promise is not possible to cancel we resolved this problem by switching to observables and introduced RxJS and redux-observables. Then request is canceled when new one is started. If you want I can provide example.

@vKongv
Copy link

vKongv commented May 23, 2018

So this is my code handling onChange and fetchRequested:

    onChange(e, { newValue, method }) {
        if (method === 'type') {
            this.setState({
                value: newValue,
            });
            fetchData(newValue);
        }
    }
    onSuggestionsFetchRequested({ value }) {
        fetchData(value);
    }

The only place where we set the input state is in onChange and will this actually introduce the race condition?

It will be great if you can provide me some examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants