我正在尝试将一个导航至其他模块中的屏幕的功能并将其导出,但是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"
}
});
}
}
};
谢谢
检查此https://reactnavigation.org/docs/navigating-without-navigation-prop/
您可以保存对NavigationContainer的引用,并使用它进行导航。