When I type something into TextInput, then I touch one of FlatList items first time. It should console.log('item press')
, but it not. Only the second touch It consoles. Does someone know the reason?
This is my code.
<TextInput
placeholder='test'
value={this.state.inputText}
onChangeText={(inputText) => this.setState({inputText})}
style={{
marginBottom: 20,
fontSize: 17,
width: 300,
textAlign: 'center''
}}
/>
<FlatList
data={[{key: 'item 1'}, {key: 'item 2'}]}
renderItem={({item}) =>
<TouchableHighlight
onPress={() => console.log('item press')}
underlayColor='#dddddd'
>
<View style={{height: 40}}>
<Text style={{fontSize: 16, textAlign: 'center'}}>{item.key}</Text>
</View>
</TouchableHighlight>
}
/>
The more complex your components are, the slower they will render. Try to avoid a lot of logic and nesting in your list items. If you are reusing this list item component a lot in your app, create a component only for your big lists and make them with as little logic and nesting as possible.
Meet FlashList! A FlatList alternative that runs on UI thread and, as they claim on their website, it runs 10x faster on JS and 5x faster on JS thread. Even considering only half the improvement — these are really great performance boosts. pod install if you need to replace your existing <FlatList> with <FlashList>.
It extracts the unique key name and its value and tells the FlatList component to track the items based on that value. The warning will then disappear after this step.
FlatList has a prop called extraData that basically re-renders the FlatList whenever there is any change using state. This feature re-renders our selectItem and renderItem function and highlights the selected items.
You should use the FlatList
with keyboardShouldPersistTaps={'handled'}
prop and handle your keyboard close in another function by Keyboard.Dissmiss()
. your FlatList
will be like this:
<FlatList
keyboardShouldPersistTaps={'handled'}
data={[{key: 'item 1'}, {key: 'item 2'}]}
renderItem={({item}) =>
<TouchableHighlight onPress={() => console.log('item press')}
underlayColor='#dddddd'>
<View style={{height: 40}}>
<Text style={{fontSize: 16, textAlign: 'center'}}>{item.key}</Text>
</View>
</TouchableHighlight>
}
/>
You can use the Keyboard.dismiss()
function in the onPress
prop after in the console.log('item press')
command in TouchableHighlight
component.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With