How to navigate from one screen with FlatList to another screen

Viewed 75

Im pretty new to this entire coding stuff and now I've finally arrived to my first unsolvable code-related myth! My problem is that I simply cannot find a way to use FlatList in a way that makes it possible for me to navigate to another screen! (I am using Stack Screen with each item of my 8 item long FlatList being linked to another screen, or at least I try to get there) Here is the code of my screen containing the FlatList:

import React, { Component } from 'react';
import {
    FlatList,
    StyleSheet, Text,
    View
} from 'react-native';
import 'react-native-gesture-handler';
import { Card } from 'react-native-paper';
import { withNavigation } from 'react-navigation';
//export default

class listTest extends Component {
    constructor(props) {
        super(props);
        this.state = {
            helpersList: [
                { ID: 1, title: ' Memories' },
                { ID: 2, title: ' Cute Cats' },
                { ID: 3, title: ' Funny Vids' },
                { ID: 4, title: ' Vegan Recipes' },
                { ID: 5, title: ' Random Jokes' },
                { ID: 6, title: ' Random Knowledge' },
                { ID: 7, title: ' Hydration' },
                { ID: 8, title: ' Memes' },
            ]
        };
    }

    navFunction = (item) => {
        //const navigation = useNavigation();

        if (item.ID === 1) {
            navigation.navigate(' Nice Memories')
            //alert('Whats Up Slappers')
        }
        else if (item.ID === 2) {
            navigation.navigate(' Cute Cats')
            //alert('Whats Up Slappers 2')
        }
        else if (item.ID === 3) {
            navigation.navigate(' Funny Vids')
        }
        else if (item.ID === 4) {
            navigation.navigate(' Vegan Recipes')
        }
        else if (item.ID === 5) {
            navigation.navigate(' Random Jokes')
        }
        else if (item.ID === 6) {
            navigation.navigate(' Random Knowledge')
        }
        else if (item.ID === 7) {
            navigation.navigate(' Hydration')
        }
        else if (item.ID === 8) {
            navigation.navigate(' Memes')
        }
    }

    render() {
        return (
            <View>
                <FlatList
                    data={this.state.helpersList}
                    renderItem={({ item }) =>
                        <Card style={styles.card} onPress={() => this.navFunction(item)}>
                            <View>
                                <Text style={styles.cardText}>
                                    {item.title}
                                </Text>
                            </View>
                        </Card>
                    }
                    keyExtractor={item => item.ID}
                />
            </View>
        )
    }
}

const styles = StyleSheet.create({
    card: {
        borderRadius: 10,
        backgroundColor: '#146b3a',
        paddingHorizontal: 15,
        paddingVertical: 40,
        marginVertical: 5,
        marginHorizontal: 16,
        shadowColor: "black",
        shadowOffset: { width: 0, height: 0, },
        shadowOpacity: 1,
        shadowRadius: 5,
    },
    cardText: {
        color: "white",
        fontSize: 20,
    }
})

export default withNavigation(listTest);

Right now the main issue I get is, that the variable 'navigation' cannot be found! Ive tried multiple navigation methods but I couldn't get a single one to work. Now I got stuck with 'withNavigation' while exporting but it also doesn't seem to be right!

Thanks for your time and help!

1 Answers

useNavigation is a hook that gives you access to the navigation object. This gives you access to the navigation object in Functional Component also. You can check their documentation.

withNavigation is a Higher Order Component (hoc) which gives the navigation object access through props. So if you're wrapping your component with withNavigation, you can access it through props. Official documentation.

In your navFunction, you can use something like this.

const { navigation } = this.props;
navigation.navigate(' Nice Memories')
.
.
navigation.navigate(' Cute Cats')
Related