Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When textInput focused, first touch on flatList doesn't work, however the second works

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>
    }
/>
like image 361
Ager Avatar asked Aug 23 '17 08:08

Ager


People also ask

How do you optimize a FlatList performance?

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.

What can I use instead of FlatList?

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>.

What does the key extractor prop of the FlatList do?

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.

How do you highlight and select multiple items in a FlatList?

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.


1 Answers

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.

like image 149
Vahid Boreiri Avatar answered Oct 04 '22 00:10

Vahid Boreiri