Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

navigate inside renderItem of Flatlist

I have a component InventoryScreen, which holds a Flatlist like this:

 <FlatList
            numColumns={2}
            data={filteredData}
            keyExtractor={(item, index) => index.toString()}
            renderItem={renderItem}
            stickyHeaderIndices={[0]}
            keyboardDismissMode={"on-drag"}>
</FlatList>
<TouchableOpacity
      onPress={() => navigation.navigate("ItemScreen") }> // <--- Here, it works.
</TouchableOpacity


Inside InventoryScreen, I can use navigation.

renderItem:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const renderItem = ({ item }: { item: ItemResponse }) => {
  return <ItemView item={item} />;
};

ItemView

const ItemView = ({ item }: { item: ItemResponse }) => {
  return (
<TouchableOpacity
      onPress={() => navigation.navigate("ItemScreen") }> // <--- This is what I want.
</TouchableOpacity

But, because there is no navigation available in ItemView, I cant navigate inside there. When trying to pass navigation to the renderItem gives me an error inside the FlatList:

Property 'navigation' is missing in type 'ListRenderItemInfo<ItemResponse>' but required in type '{ item: ItemResponse; navigation: any; }'.

How can I access navigation inside ItemView ?

>Solution :

Please try this
Root Component

const RootComp = ({navigation})=>{

    const renderItem = ({ item }: { item: ItemResponse }) => {
      return <ItemView item={item} navigation={navigation} />;
    };

return (
<FlatList
            numColumns={2}
            data={filteredData}
            keyExtractor={(item, index) => index.toString()}
            renderItem={renderItem}
            stickyHeaderIndices={[0]}
            keyboardDismissMode={"on-drag"}>
</FlatList>
)

}

ItemView

const ItemView = ({ item, navigation }: { item: ItemResponse }) => {
  return (
<TouchableOpacity
      onPress={() => navigation.navigate("ItemScreen") }> // <--- This is what I want.
</TouchableOpacity
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading