You need to enable JavaScript to run this app. react-native-autocomplete-dropdown无法选中下拉列表项问题
优惠活动
大模型
产品
解决方案
定价
更多

react-native-autocomplete-dropdown无法选中下拉列表项问题

react-native-autocomplete-dropdown 下拉选项无法选中问题排查

问题复现

使用react-native-autocomplete-dropdown组件时,出现下拉列表内所有选项无法点击选中的异常,对应实现代码如下:

<View style={{flex: 0.97, flexDirection: 'column', marginLeft: 10}}>
  <View style={{flexDirection: 'row'}}>
    <Text
      style={{
        fontFamily: 'Titillium-Semibold',
        color: Colors.grey_888888,
        fontSize: 14,
      }}>
      Dropdown Items
    </Text>
    <Text
      style={{
        fontFamily: 'Titillium-Semibold',
        color: Colors.grey_888888,
        fontSize: 14,
      }}>
      *
    </Text>
  </View>
  <AutocompleteDropdown
    clearOnFocus={false}
    closeOnBlur={true}
    closeOnSubmit={false}
    onSelectItem={item => {
      item && setCustomerID(item.id);
    }}
    dataSet={[
      {id: '1', title: 'Alpha'},
      {id: '2', title: 'Beta'},
      {id: '3', title: 'Gamma'},
    ]}
    renderItem={(item, text) => (
      <Text style={{color: Colors.black, padding: 15}}>
        {item.title}
      </Text>
    )}
    inputContainerStyle={styles.dropdown}
  />
</View>

组件运行效果:
组件运行截图

根因分析

核心原因是自定义renderItem时遗漏了点击事件绑定:
组件默认渲染的列表项自带可点击容器和选中触发逻辑,当传入自定义renderItem覆盖默认渲染后,返回的纯<Text>组件没有绑定选中回调、也没有可点击的交互容器,无法触发组件内部的onSelectItem逻辑,表现为点击选项无任何响应。
小概率触发原因包括:下拉列表层被其他同级/父级元素覆盖拦截点击、closeOnBlur配置导致失焦关闭时机早于点击事件触发。

修复方案

  • 无强定制需求时,直接删除代码中的renderItem属性,使用组件内置的列表项渲染逻辑,即可恢复正常选中能力
  • 需要自定义列表项样式时,正确接收renderItem回调透传的选中方法,用可点击组件包裹自定义内容,修正后的renderItem写法如下(需提前从react-native引入TouchableOpacity):
renderItem={(item, text, isSelected, onSelect) => (
  <TouchableOpacity
    activeOpacity={0.7}
    style={{padding: 15}}
    onPress={() => onSelect(item)}
  >
    <Text style={{color: Colors.black}}>
      {item.title}
    </Text>
  </TouchableOpacity>
)}
  • 若上述修改后仍无法选中,按顺序排查:
    1. 临时将closeOnBlur属性设为false,验证是否为输入框失焦提前关闭下拉列表拦截了点击
    2. 给AutocompleteDropdown组件添加高于同级元素的zIndex样式,排查是否有其他定位元素覆盖下拉区域拦截点击事件

内容的提问来源于stack exchange,提问作者Tafsir Ahamed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 23:54:18