• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

警惕RNTextInput控件因key不固定造成的输入后失去焦点,丢失字符等问题

武飞扬头像
平光镜后的小呆毛
帮助6

根本原因:react/react native 采用diff算法,若重新渲染时dom的key不同则会生成新dom,造成字符丢失,失去焦点等问题。

例如:

  1.  
    <SafeAreaView style={tw.style('bg-white', { flex: 1 })}>
  2.  
    <FlatList
  3.  
    contentContainerStyle={tw.style('p-5 mb-5')}
  4.  
    data={location}
  5.  
    keyExtractor={(item, index) => `${item.id}-${index}`}
  6.  
    renderItem={renderItem}
  7.  
    ListHeaderComponent={() => {
  8.  
    return (
  9.  
    <React.Fragment key={'haha'}>
  10.  
     
  11.  
    <View
  12.  
    style={{
  13.  
    paddingVertical: 3,
  14.  
    borderBottomWidth: 1,
  15.  
    borderColor: 'rgba(105,105,105,1)',
  16.  
    marginBottom: 5,
  17.  
    }}
  18.  
    >
  19.  
    <TextInput
  20.  
    style={{
  21.  
    paddingLeft: 5,
  22.  
    fontSize: 20,
  23.  
    fontFamily: 'InterRegular',
  24.  
    color: 'rgba(105,105,105,1)',
  25.  
    }}
  26.  
    placeholder="place input your destination"
  27.  
    onChangeText={searchDestination}
  28.  
    />
  29.  
    </View>
  30.  
    </React.Fragment>
  31.  
    );
  32.  
    }}
  33.  
    ListFooterComponent={() => {
  34.  
    return <View></View>;
  35.  
    }}
  36.  
    />
  37.  
    </SafeAreaView>
学新通

本例中,TextInput控件虽然没有写在内容区而是写在了listHeader区,但是每次刷新仍然会出问题。

解决方法应该是将TextInput写到FlatList外。或者通过代码令其不重复渲染。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhajai
系列文章
更多 icon
同类精品
更多 icon
继续加载