在普通函数中使用react react.navigate-(不允许使用钩子,即useNavigation)?

问题描述 投票:0回答:1

我正在尝试将一个导航至其他模块中的屏幕的功能并将其导出,但是navigation不起作用。我尝试使用UseNavigation(),但出现错误,即:Unhandled promise rejection: Invariant Violation: Hooks can only be called inside the body of a function component.

是否可以在常规功能或其他任何功能中使用导航。

import React, { useState, useEffect, useCallback } from "react";
import { AsyncStorage, Alert } from "react-native";
import { useNavigation } from "react-navigation-hooks";

export const startMixGame = async (categoryIsChosen, withTimer) => {
  const navigation = useNavigation();

  if (categoryIsChosen) {
    if (withTimer) {
      await AsyncStorage.setItem("useTimer", "true");
      navigation.navigate({
        routeName: "MixedQuestions",
        params: {
          categoryId: "1"
        }
      });
    } else if (!withTimer) {
      // console.log("withTimer", withTimer);

      await AsyncStorage.setItem("useTimer", "false");
      navigation.navigate({
        routeName: "NoTimerMixedQuestions",
        params: {
          categoryId: "1"
        }
      });
    }
  }

};

谢谢

reactjs react-native react-navigation react-native-navigation
1个回答
0
投票

检查此https://reactnavigation.org/docs/navigating-without-navigation-prop/

您可以保存对NavigationContainer的引用,并使用它进行导航。

© www.soinside.com 2019 - 2024. All rights reserved.