React本地TextInput在我键入一个字符之后一直不关注和刷新整个页面

发布于 2025-01-23 18:33:16 字数 1598 浏览 2 评论 0原文

return(
        <View style={styles.container}>
      <View style={styles.searchbarContainer}>
        <TouchableOpacity style={styles.ThreeLineButton} onPress={returnHome}>
          <Text style={{color: 'white', fontSize: 20,}}>Done</Text>
        </TouchableOpacity>
        {/* <Text style={{color: 'white', fontSize: 50, marginLeft: 50}}>UStudy</Text> */}
        {/* <TextInput style={styles.searchbarStyle} placeholder="Search Ustudy" placeholderTextColor='#FFF' onChangeText={setSearchText} value={searchText} onChange={refreshSearch} /> */}
        
      </View>
      <ScrollView
      refreshControl={
          <RefreshControl
            refreshing={refreshing}
            onRefresh={onRefresh}
          />
        }
      >
        <FlatList
                ref={(ref) => { this.commentListRef = ref; }}
                data={allComments}
                renderItem={({item}) => <CommentCard item={item} />}
                keyExtractor={item=>item.docID}
                showsVerticalScrollIndicator={false}
                style={{
                width: '110%',
                }}

                refreshControl={
                <RefreshControl
                    refreshing={refreshing}
                    onRefresh={onRefresh}
                />
                }
            />
      </ScrollView>
      </View>
    );
};

export default DynamicPostScreen

这是我的UI的代码以及如何嵌入TextInput。每当我在文本输入中键入字符时,它都会刷新整个页面,并从文本输入中不关注。如何阻止这种情况发生?

return(
        <View style={styles.container}>
      <View style={styles.searchbarContainer}>
        <TouchableOpacity style={styles.ThreeLineButton} onPress={returnHome}>
          <Text style={{color: 'white', fontSize: 20,}}>Done</Text>
        </TouchableOpacity>
        {/* <Text style={{color: 'white', fontSize: 50, marginLeft: 50}}>UStudy</Text> */}
        {/* <TextInput style={styles.searchbarStyle} placeholder="Search Ustudy" placeholderTextColor='#FFF' onChangeText={setSearchText} value={searchText} onChange={refreshSearch} /> */}
        
      </View>
      <ScrollView
      refreshControl={
          <RefreshControl
            refreshing={refreshing}
            onRefresh={onRefresh}
          />
        }
      >
        <FlatList
                ref={(ref) => { this.commentListRef = ref; }}
                data={allComments}
                renderItem={({item}) => <CommentCard item={item} />}
                keyExtractor={item=>item.docID}
                showsVerticalScrollIndicator={false}
                style={{
                width: '110%',
                }}

                refreshControl={
                <RefreshControl
                    refreshing={refreshing}
                    onRefresh={onRefresh}
                />
                }
            />
      </ScrollView>
      </View>
    );
};

export default DynamicPostScreen

here is the code of my UI and how the TextInput is embeded. Whenever I type a character in my TextInput it refreshes the whole page and unfocuses from the TextInput. How do I stop this from happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掐死时间 2025-01-30 18:33:16

不幸的是,您没有提供组件的完整代码。我建议您对组件进行重新处理,并给予适当的格式,尤其是凹痕。

另外,您通常不希望在滚动浏览中列入列表,那里可能有一个问题。

关于问题本身,没有完整的代码,我不能100%确定。

但是,您的setSearchText可能会触发USESTATE变化,因此可以重新启动组件(这就是状态的作用)。

这是一个状态管理问题,您可以将TextInput移至单独的组件并从那里管理状态,以使父组件无法启用。

另一种可能性是,通过使用OnchangeText和Onchange,您可能会触发渲染,可能会使用RefreshSearch函数。

如果这没有帮助,请在一开始就按照我的指示进行操作,我很高兴再次查看。

Unfortunately, you didn't provide the full code of the component. I recommend you refactor the component and give it proper formatting, especially the indentation.

Also, you usually don't want a FlatList inside a ScrollView, there is probably an issue there.

About the question itself, I can't be 100% sure without the full code.

But it's possible that your setSearchText is triggering a useState change and therefore rerendering the component (that's what a state does).

This is a state management problem and you could move the TextInput to a separate component and manage the state from there so the parent component won't rerender.

Another possibility is that by using onChangeText and onChange you are triggering the render, probably with the refreshSearch function.

If this didn't help, please follow my instructions at the beginning and I'm happy to look at it again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文