(NOBRIDGE) ERROR Warning: Error: useAuth must be used within an AuthProvider
This error is located at:
in RootLayout
in Unknown (created by Route())
in Suspense (created by Route())
in Route (created by Route())
in Route() (created by ContextNavigator)
in RNCSafeAreaProvider (created by SafeAreaProvider)
in SafeAreaProvider (created by wrapper)
in wrapper (created by ContextNavigator)
in ThemeProvider
in EnsureSingleNavigator
in BaseNavigationContainer
in NavigationContainerInner (created by ContextNavigator)
in ContextNavigator (created by ExpoRoot)
in ExpoRoot (created by App)
in App (created by ErrorOverlay)
in ErrorToastContainer (created by ErrorOverlay)
in ErrorOverlay (created by withDevTools(ErrorOverlay))
in withDevTools(ErrorOverlay)
in RCTView (created by View)
in View (created by AppContainer)
in RCTView (created by View)
in View (created by AppContainer)
in AppContainer
in main(RootComponent)
Согласно этому сообщению, перехват useAuth должен использоваться в дочернем элементе, обернутом контекстом AuthProvider, что я и сделал здесь:
App.tsx:
Эта проблема возникла у меня уже довольно давно, я пробовал все, что мог, чтобы ее исправить, но ничего не помогло. Вот содержимое AuthContext.tsx: [code]import React, { createContext, useContext, useEffect, useState, ReactNode, } from "react"; import axios from "axios"; import * as SecureStore from "expo-secure-store"; import { ActivityIndicator } from "react-native-paper"; import { jwtDecode } from "jwt-decode";
const useAuth = (): AuthContextType => { const context = useContext(AuthContext); if (!context) { throw new Error("useAuth must be used within an AuthProvider"); } return context; };
type AuthProviderProps = { children: ReactNode; };
export { AuthProvider, useAuth }; [/code] Это выглядит довольно солидно и должно работать, но я продолжаю получать [code](NOBRIDGE) ERROR Warning: Error: useAuth must be used within an AuthProvider
This error is located at: in RootLayout in Unknown (created by Route()) in Suspense (created by Route()) in Route (created by Route()) in Route() (created by ContextNavigator) in RNCSafeAreaProvider (created by SafeAreaProvider) in SafeAreaProvider (created by wrapper) in wrapper (created by ContextNavigator) in ThemeProvider in EnsureSingleNavigator in BaseNavigationContainer in NavigationContainerInner (created by ContextNavigator) in ContextNavigator (created by ExpoRoot) in ExpoRoot (created by App) in App (created by ErrorOverlay) in ErrorToastContainer (created by ErrorOverlay) in ErrorOverlay (created by withDevTools(ErrorOverlay)) in withDevTools(ErrorOverlay) in RCTView (created by View) in View (created by AppContainer) in RCTView (created by View) in View (created by AppContainer) in AppContainer in main(RootComponent) [/code] Согласно этому сообщению, перехват useAuth должен использоваться в дочернем элементе, обернутом контекстом AuthProvider, что я и сделал здесь: App.tsx: [code]import React from "react"; import { AuthProvider } from "@/context/AuthContext"; import RootLayout from "./app/_layout"; import { NotifierWrapper } from "react-native-notifier"; import { GestureHandlerRootView } from "react-native-gesture-handler";
// Wrap the entire app with AuthProvider export default function App() { return (
); } [/code] И ./app/_layout.tsx: [code]import React, { useCallback } from "react"; import { Slot, Redirect } from "expo-router"; import { GestureHandlerRootView } from "react-native-gesture-handler"; import { useFonts } from "expo-font"; import * as SplashScreen from "expo-splash-screen"; import { useAuth } from "@/context/AuthContext"; import { ActivityIndicator } from "react-native-paper";
// Prevent the splash screen from hiding until the font is loaded SplashScreen.preventAutoHideAsync();
export default function RootLayout() { const { authState } = useAuth(); // Directly access authState from context
if (!fontsLoaded) { return null; // Keep splash screen visible while loading fonts }
// If authState is undefined, don't attempt to render protected routes yet if (authState === undefined) { return ; // Show loading indicator if auth state is undefined }
return (
{authState.authenticated ? : }
); } [/code] Это должно работать, потому что я обернул RootLayout с помощью AuthProvider. Я что-то сделал не так?