0

I need to access the navigate prop in a class component to navigate to another page however that's not possible outside a functional component. And I'm having trouble trying to use the workaround from the docs.

Current code is just returning an error console error

I have a feeling I am just misunderstanding the docs in this case. Any help would be huge!

Class Component

import { firebase } from "@react-native-firebase/auth";
import { NavigationContainer, useNavigation } from "@react-navigation/native";
import {
  Box,
  Button,
  Divider,
  HStack,
  Heading,
  ScrollView,
  Spinner,
  Text,
} from "native-base";
import React from "react";

const user = firebase.auth().currentUser;

function getIsQuestCompleted(querySnapshot: any) {
  return querySnapshot.get("key");
}

class TaskTrackingComp extends React.Component {
  state = {
    savedKey: [],
    ttrkerData: [],
    loading: true,

  };

  getTarkovTrackerData = async () => {
    await fetch("https://tarkovtracker.io/api/v2/progress", {
      method: "GET",
      headers: {
        Authorization: `Bearer ${this.state.savedKey}`,
      },
    })
      .then((res) => res.json())
      .then((data) =>
        this.setState({
          ttrkerData: data,
          loading: false,
        })
      );
  };

  componentDidMount() {
    firebase
      .firestore()
      .collection("UserApiKeys")
      .doc(user?.uid)
      .get()
      .then((querySnapshot) => getIsQuestCompleted(querySnapshot))
      .then((savedKey) => {
        console.log("Saved Key", savedKey),
          this.setState({
            savedKey,
          }),
          this.getTarkovTrackerData();
      });
  }
  render() {
    const { navigation } = this.props;
    console.log("savedKey state:", this.state.savedKey);
    console.log("Tarkov Tracker Data state:", this.state.ttrkerData);
    console.log("Loading?", this.state.loading);
    return (
      <ScrollView>
        <Box justifyContent={"center"} alignItems={"center"}>
          <Heading fontWeight={"extrabold"}>Tarkov Tracker Stats</Heading>
        </Box>
        <Divider />
        {this.state.loading ? (
          <HStack space={2} justifyContent={"center"} alignItems={"center"}>
            <Box>
              <Text>Getting Stats</Text>
            </Box>
            <Box>
              <Spinner color="warning.500" />
            </Box>
          </HStack>
        ) : (
          <Box justifyContent={"center"} alignItems={"center"}>
            <Button onPress={() => navigation.navigation("Home")}></Button>
          </Box>
        )}
      </ScrollView>
    );
  }
}

export default function TaskTracking(props: any) {
  const navigation = useNavigation();
  return <TaskTrackingComp {...props} navigation={navigation} />;
}
0

2 Answers 2

1

This was a mistake on my part I did not realise the export was a default and that was causing the error!

Sign up to request clarification or add additional context in comments.

Comments

0

try changing onPress function of Button to this navigation.navigate("Home")

1 Comment

I appreciate the speedy response however it's okay now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.