Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

react native - I need to delete a key from a dict every time i have to update it

I am working with flatList in react-native. I am also using ListHeaderComponent prop and i am setting an TextInput inside that.

Here is code;

<FlatList ListHeaderComponent={() => <TextInput value={this.newState.value} onChangeText={this.getSearch} />} renderItem={this.renderItem} onEndEditing={this.setSearch} />

As flatList re-renders when state is changed, which closes the keyboard after every key press.To avoid that, i created a new dict object namely newState to store the text written in the textInput and i am changing state after the user ends typing by using the prop onEndEdititng;

Here is code;

newState = {}

getSearch = search => {
    this.newState.value = search;
}

Now to change the state which is important for my code;

setSearch = () => {
this.setState({search: this.newState.value})
}

Now the problem is that the newState updates only at first i.e, when i provide the first search, it works fine, but if i try to change the search inside the textInput, it is not changed and only one letter is added or removed if it try to add or remove multiple letters and this change is also visible only when i end editing.

Now to solve this problem, i tried to delete the value key from the newState dict every time i am done. Inside setSearch i added this line;

delete this.newState.value;

and that worked fine. But i want to know, why the dict is updating only once, i.e, at the first attempt and not after that and why i need to delete the key every time i make a new search.

Please comment if my question is not clear.

question from:https://stackoverflow.com/questions/66061591/i-need-to-delete-a-key-from-a-dict-every-time-i-have-to-update-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...