onmotion / react-native-autocomplete-dropdown

Dropdown Item picker with search and autocomplete (typeahead) functionality for react native
MIT License
201 stars 79 forks source link

Suggestion list is removed when more than one dropdown is available #110

Open lorenc-tomasz opened 8 months ago

lorenc-tomasz commented 8 months ago

Hi, I have noticed issue with multiple dropdown when at least one of have onChangeText implemented and it changes parent's state.

  1. Add at least two dropdowns within the same component.
  2. Implement onChangeText that will set state e.g.
onChangeText={(text) => {
setState(text); 
}}
  1. Setting the state will cause re-render of component with dropdowns.
  2. This useEffect will be called for each dropdown:
    useEffect(() => {
      if (isOpened && Array.isArray(dataSet)) {
        if (activeInputRef) {
          activeInputRef.current = containerRef.current
        }

        setContent(
          <Dropdown
            {...{
              ...props,
              direction,
              inputHeight,
              dataSet,
              suggestionsListMaxHeight,
              renderItem,
              ListEmptyComponent
            }}
          />
        )
      } else {
        setContent(undefined)
      }
    }, [
      isOpened,
      dataSet,
      props,
      direction,
      inputHeight,
      suggestionsListMaxHeight,
      renderItem,
      ListEmptyComponent,
      activeInputRef,
      setContent
    ])
  1. As other dropdowns will not be openend the setContent(undefined) will be called and suggestion list will be hidden from the view.

  2. The easiest fix I have found was to add check to mentioned useEffect :


    const close = () => {
      setIsOpened(false)
      setDirection(props.direction)
      setContent(undefined)
      activeInputRef.current = null <-- reset active ref
    }

    useEffect(() => {
      // check if active ref is the correct one
      if (activeInputRef.current && activeInputRef.current !== containerRef.current) {
        return;
      }

      if (isOpened && Array.isArray(dataSet)) {
        if (activeInputRef) {
          activeInputRef.current = containerRef.current
        }

with this check useEffect will not be called for not active input.

Can you please look into this, please?

harshilmobmaxime commented 7 months ago

@lorenc-tomasz all previous changes, change on node_modules/react-native-autocomplete-dropdown then run command "yarn patch-package react-native-autocomplete-dropdown" that can be work for me

imarsiglia commented 7 months ago

I have the same problem and need to use 5 Dropdowns in the same view. When searching in autocomplete the application becomes extremely slow and the list appears for a few seconds and then disappears. @lorenc-tomasz able to find any solution?

vijayanandof commented 1 month ago

@onmotion Any updates or work around for this bug?

onmotion commented 1 month ago

@vijayanandof I'll try to reproduce