最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to make my app show the user the authentication group or the normal app when I am using react-redux for state management? -

programmeradmin3浏览0评论

I am trying to use the user state to determine if my app shows the authentication group or not, but now I understand that the setup that I have cannot work because the the root layout component cannot access the state.

How can get accomplish my goal?

import {
  DarkTheme,
  DefaultTheme,
  ThemeProvider,
} from "@react-navigation/native";
import { useFonts } from "expo-font";
import { Stack } from "expo-router";
import { Drawer } from "expo-router/drawer";
import * as SplashScreen from "expo-splash-screen";
import { StatusBar } from "expo-status-bar";
import { useEffect } from "react";
import "react-native-reanimated";
import { useColorScheme } from "@/hooks/useColorScheme";
import { selectCurrentUser } from "@/store/auth/authSlice";
import { useSelector } from "react-redux";
import "react-native-gesture-handler";
import { Provider } from "react-redux";
import store from "../store/store";

// Prevent the splash screen from auto-hiding before asset loading is complete.
SplashScreen.preventAutoHideAsync();

export default function RootLayout() {
  const currentUser = useSelector(selectCurrentUser);
  const colorScheme = useColorScheme();
  const [loaded] = useFonts({
    SpaceMono: require("../assets/fonts/SpaceMono-Regular.ttf"),
  });

  useEffect(() => {
    if (loaded) {
      SplashScreen.hideAsync();
    }
  }, [loaded]);

  if (!loaded) {
    return null;
  }

  return (

    <Provider store={store}>
      <ThemeProvider value={colorScheme === "dark" ? DarkTheme : DefaultTheme}>
        {currentUser ? (
          <Drawer>
            <Drawer.Screen name="(tabs)" options={{ headerShown: false }} />
            <Drawer.Screen name="+not-found" />
          </Drawer>
        ) : (
          <Stack>
            <Stack.Screen
              name="(authentication)"
              options={{ headerShown: false }}
            />
          </Stack>
        )}

        <StatusBar style="auto" />
      </ThemeProvider>
    </Provider>
  );
}

I tried using select current user but I got this error: could not find react-redux context value

I am trying to use the user state to determine if my app shows the authentication group or not, but now I understand that the setup that I have cannot work because the the root layout component cannot access the state.

How can get accomplish my goal?

import {
  DarkTheme,
  DefaultTheme,
  ThemeProvider,
} from "@react-navigation/native";
import { useFonts } from "expo-font";
import { Stack } from "expo-router";
import { Drawer } from "expo-router/drawer";
import * as SplashScreen from "expo-splash-screen";
import { StatusBar } from "expo-status-bar";
import { useEffect } from "react";
import "react-native-reanimated";
import { useColorScheme } from "@/hooks/useColorScheme";
import { selectCurrentUser } from "@/store/auth/authSlice";
import { useSelector } from "react-redux";
import "react-native-gesture-handler";
import { Provider } from "react-redux";
import store from "../store/store";

// Prevent the splash screen from auto-hiding before asset loading is complete.
SplashScreen.preventAutoHideAsync();

export default function RootLayout() {
  const currentUser = useSelector(selectCurrentUser);
  const colorScheme = useColorScheme();
  const [loaded] = useFonts({
    SpaceMono: require("../assets/fonts/SpaceMono-Regular.ttf"),
  });

  useEffect(() => {
    if (loaded) {
      SplashScreen.hideAsync();
    }
  }, [loaded]);

  if (!loaded) {
    return null;
  }

  return (

    <Provider store={store}>
      <ThemeProvider value={colorScheme === "dark" ? DarkTheme : DefaultTheme}>
        {currentUser ? (
          <Drawer>
            <Drawer.Screen name="(tabs)" options={{ headerShown: false }} />
            <Drawer.Screen name="+not-found" />
          </Drawer>
        ) : (
          <Stack>
            <Stack.Screen
              name="(authentication)"
              options={{ headerShown: false }}
            />
          </Stack>
        )}

        <StatusBar style="auto" />
      </ThemeProvider>
    </Provider>
  );
}

I tried using select current user but I got this error: could not find react-redux context value

Share Improve this question asked Jan 19 at 1:20 Wagner GauerWagner Gauer 331 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The RootLayout component is the component rendering the Redux Provider component that provides the Redux context, so it itself cannot use the useSelector hook. Push the Provider higher up the ReactTree, or push the currentUser ? .... logic lower down the ReactTree.

Examples:

  • Push Provider up the ReactTree

    import { Provider } from "react-redux";
    import store from "../store/store";
    
    ...
    
    <Provider store={store}>
      <RootLayout />
    </Provider>
    
    export default function RootLayout() {
      const currentUser = useSelector(selectCurrentUser);
      const colorScheme = useColorScheme();
      const [loaded] = useFonts({
        SpaceMono: require("../assets/fonts/SpaceMono-Regular.ttf"),
      });
    
      useEffect(() => {
        if (loaded) {
          SplashScreen.hideAsync();
        }
      }, [loaded]);
    
      if (!loaded) {
        return null;
      }
    
      return (
        <ThemeProvider value={colorScheme === "dark" ? DarkTheme : DefaultTheme}>
          {currentUser ? (
            <Drawer>
              <Drawer.Screen name="(tabs)" options={{ headerShown: false }} />
              <Drawer.Screen name=" + not-found" />
            </Drawer>
          ) : (
            <Stack>
              <Stack.Screen
                name="(authentication)"
                options={{ headerShown: false }}
              />
            </Stack>
          )}
          <StatusBar style="auto" />
        </ThemeProvider>
      );
    }
    
  • Push the consumer down the ReactTree

    const App = () => {
      const currentUser = useSelector(selectCurrentUser);
    
      return currentUser
        ? (
          <Drawer>
            <Drawer.Screen name="(tabs)" options={{ headerShown: false }} />
            <Drawer.Screen name=" + not-found" />
          </Drawer>
        ) : (
          <Stack>
            <Stack.Screen
              name="(authentication)"
              options={{ headerShown: false }}
            />
          </Stack>
        );
    };
    
    export default function RootLayout() {
      const colorScheme = useColorScheme();
      const [loaded] = useFonts({
        SpaceMono: require("../assets/fonts/SpaceMono-Regular.ttf"),
      });
    
      useEffect(() => {
        if (loaded) {
          SplashScreen.hideAsync();
        }
      }, [loaded]);
    
      if (!loaded) {
        return null;
      }
    
      return (
        <Provider store={store}>
          <ThemeProvider value={colorScheme === "dark" ? DarkTheme : DefaultTheme}>
            <App />
            <StatusBar style="auto" />
          </ThemeProvider>
        </Provider>
      );
    }
    
发布评论

评论列表(0)

  1. 暂无评论