Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript React Native Flatlist: How to give renderItem the correct type of it's item?

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?

like image 615
J. Hesters Avatar asked Oct 08 '18 11:10

J. Hesters


People also ask

How do I get an index from FlatList?

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.

What kind of data does the FlatList component expect to be passed to its data props?

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.

Why we use keyExtractor in FlatList React Native?

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.


1 Answers

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} />   ); 
like image 114
Yhozen Avatar answered Sep 19 '22 14:09

Yhozen