I'm building a React Native app with TypeScript. renderItem
complains that the destructured item implicitly has an any
type. I googled and found this question and tried to implement what they teach here combined with the types in index.d.ts
of the @types
package for React Native.
export interface Props { emotions: Emotion[]; } class EmotionsPicker extends PureComponent<Props> { keyExtractor = (item, index) => index; renderItem = ({ item }) => ( <ListItem title={item.name} checkmark={item.checked} /> ); render() { return ( <FlatList<Emotion> keyExtractor={this.keyExtractor} renderItem={this.renderItem} data={this.props.emotions} /> ); } }
Unfortunately this does not work. How can I give item the type Emotion
?
To get the index of the currently visible item in a React Native flat list, we can set the onViewableItemsChange prop to a function that gets the current visible items. to define the onViewableItemsChanged function that gets the viewableItems property.
The FlatList component takes two required props: data and renderItem. The data is the source of elements for the list, and renderItem takes one item from the source and returns a formatted component to render.
keyExtractor Used to extract a unique key for a given item at the specified index. Key is used for caching and as the react key to track item re-ordering.
I know it's an old question but people googling it might still appreciate it.
import { FlatList, ListRenderItem } from 'react-native' /* . . . */ renderItem: ListRenderItem<Emoticon> = ({ item }) => ( <ListItem title={item.name} checkmark={item.checked} /> );
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