Skip to content

maxResults prop changes aren't immediately honored #723

Closed
@FilmCoder

Description

@FilmCoder

Steps to reproduce

  1. Click the "one" button
  2. Click the typeahead input
  3. Click the "states" button
  4. Click the typeahead input again, notice only one US State shows

https://codesandbox.io/s/rbt-floating-labels-forked-bd8x8n?file=/src/index.js

Expected Behavior

If themaxResults prop is changed, the typeahead input should honor the new maxResults, not the old. For instance, right now if you pass an options array of 1 length along with maxResults=1, click on the input, then if you programmatically change the options and maxResults props to something else, like a longer options array with maxResults=7, then when clicking on the input it only shows 1 suggestion.

It seems this is a bug with derived state. I hate derived state, tricky and bug-prone but sometimes necessary. It looks like shownResults is added to state initially in getInitialState: https://github.com/ericgio/react-bootstrap-typeahead/blob/main/src/core/Typeahead.tsx#L244

But then only updated in certain circumstances, such as when the input changes in in _handleInputChange in Typeaheadmanager:
https://github.com/ericgio/react-bootstrap-typeahead/blob/main/src/core/Typeahead.tsx#L244

I think the best way to deal with this is update state in getDerivedStateFromProps, not during input changes. That way state updates immediately on prop change, instead of only on input change.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions