Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6291

Typescript: Assign prop type to component prop in a custom component in React Native

$
0
0

I am creating a custom reusable component which can take in another Component as a prop to used as a children. I want to add the type dynamically form the data which is passed into this component.

The best reference is <FlatList>. I need exactly how <FlatList> is created. We have a renderItem prop for it where it will take in that component and pass the data's type to the renderItem function.

Example:

<FlatList    data={['str1', 'str2', 'str3']} // Array type is string[] here    renderItem={({item}) => <Text>{item}</Text>} // The item type is string here />

Desired Component:

<MyComponent    data={[ { name: 'abc', age: 5 }, { name: 'xyz', age: 5 } ]} // Array is a custom type User[]    renderItem={({item}) => <Text>{item.name}</Text>} /> // The item is User type 

Viewing all articles
Browse latest Browse all 6291

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>