React Native TextInput setState() hides keyboard

我怕爱的太早我们不能终老 提交于 2021-01-28 00:26:19

问题


Please check on the snack link, https://snack.expo.io/@banid/textinput The TextInput on the filter view(shows when the button is pressed) hides keyboard when ever I call setState(). I call setState to update the value of TextInput. Bcause of this I can't type continuously on the TextInput. Is this a bug or am I doing something wrong?? Thank you


回答1:


The problem is that you are creating a brand new (anonymous) function that renders the header of the FlatList on every update

<FlatList .... ListHeaderComponent={() => this.showHeader()} />

So a new TextInput is being created instead of updating the existing one.

Solution:

change ListHeaderComponent={() => this.showHeader()}

to ListHeaderComponent={this.showHeader} as ListHeaderComponent can be a function

https://facebook.github.io/react-native/docs/flatlist#listheadercomponent

Similar Issue: https://github.com/react-native-training/react-native-elements/issues/559




回答2:


It seems the focus is being removed from the TextInput after every setState, here's a work around you can use:

Change

this.setState({
    filterSearchText: text,
    data: newData,
});

To

this.setState({
    filterSearchText: text,
    data: newData,
}, () => {
    this.searchInput.focus();
});

What this does is it brings back the focus to searchInput after a setState is called



来源:https://stackoverflow.com/questions/56180263/react-native-textinput-setstate-hides-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!