React上下文发生变化,但屏幕未发生变化。

huangapple go评论79阅读模式
英文:

React context changes but it doesn't change the screen

问题

以下是您要翻译的部分:

"I am creating an app with react and I created a login system with ReactContext where there is a variable named userToken that allows the user to change from the stackNavigator of the login to the one of the main app because I created a stack with some screens for the login and another stack with other screens. When clicked on the button located in the login page it should hange the value of the variable and change screens but somehow it doesn't work."

"This is the ReactContext"

import React, { useState } from "react";

export const AppContext = React.createContext();

export function Context({ children }) {
  const [userToken, setUserToken] = useState(null);
  const [isLoading, setisLoading] = useState(false);

  const login = () => {
    setisLoading(false);
    setUserToken("iojoiajsdoijaso");
  };

  const logout = () => {
    setisLoading(false);
    setUserToken(null);
  };

  return (
    <AppContext.Provider value={{ login, logout, userToken }}>
      {children}
    </AppContext.Provider>
  );
}

"Here is the navigator in charge of changing the screens when the variable is changed"

import { FA5Style } from "@expo/vector-icons/build/FontAwesome5";
import { NavigationContainer } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import { useContext } from "react";
import { AppContext, Context } from "../Contexts/Context";
import AppStack from "../Navigation/AppStack";
import AuthStack from "../Navigation/AuthStack";
import BottomNavBar from "../Navigation/BottomNavBar";

const MainNav = createNativeStackNavigator();

function AppNav() {
  const userToken = useContext(AppContext);
  return (
    <Context>
      <NavigationContainer>
        {userToken == null ? <AuthStack /> : <AppStack />}
      </NavigationContainer>
    </Context>
  );
}

export default AppNav;

"and this is how I call the react context in my Login screen once the button is pressed."

<CustomButton label={"Login"} onPress={() => login()} />

"My problem is that once I click on this button in the LoginScreen the value of the variable 'userToken' changes but the trinary operator doesn't seem to work and it doesn't change the stack so the user can see the user can see the Appstack when userToken changes. I tried different variations of the trinary operator. I think the error is beacuse it's executing just one time."

"How could I fix this?"

英文:

I am creating an app with react and I created a login system with ReactContext where there is a variable named userToken that allows the user to change from the stackNavigator of the login to the one of the main app because I created a stack with some screens for the login and another stack with other screens. When clicked on the button located in the login page it should hange the value of the variable and change screens but somehow it doesn't work.

This is the ReactContext

import React, { useState } from &quot;react&quot;;

export const AppContext = React.createContext();

export function Context({ children }) {
  const [userToken, setUserToken] = useState(null);
  const [isLoading, setisLoading] = useState(false);

  const login = () =&gt; {
    setisLoading(false);
    setUserToken(&quot;iojoiajsdoijaso&quot;);
  };

  const logout = () =&gt; {
    setisLoading(false);
    setUserToken(null);
  };

  return (
    &lt;AppContext.Provider value={{ login, logout, userToken }}&gt;
      {children}
    &lt;/AppContext.Provider&gt;
  );
}

Here is the navigator in charge of changing the screens when the variable is changed

    import { FA5Style } from &quot;@expo/vector-icons/build/FontAwesome5&quot;;
    import { NavigationContainer } from &quot;@react-navigation/native&quot;;
    import { createNativeStackNavigator } from &quot;@react-navigation/native-stack&quot;;
    import { useContext } from &quot;react&quot;;
import { AppContext, Context } from &quot;../Contexts/Context&quot;;
import AppStack from &quot;../Navigation/AppStack&quot;;
import AuthStack from &quot;../Navigation/AuthStack&quot;;
import BottomNavBar from &quot;../Navigation/BottomNavBar&quot;;

const MainNav = createNativeStackNavigator();

function AppNav() {
  const userToken = useContext(AppContext);
  return (
    &lt;Context&gt;
      &lt;NavigationContainer&gt;
        {userToken == null ? &lt;AuthStack /&gt; : &lt;AppStack /&gt;}
      &lt;/NavigationContainer&gt;
    &lt;/Context&gt;
  );
}

export default AppNav;

and this is how I call the react context in my Login screen once the button is pressed.

  &lt;CustomButton label={&quot;Login&quot;} onPress={() =&gt; login()} /&gt;

My problem is that once I click on this button in the LoginScreen the value of the variable "userToken" changes but the trinary operator doesn't seem to work and it doesn't change the stack so the user can see the user can see the Appstack when userToken changes. I tried different variations of the trinary operator.
I think the error is beacuse it's executing just one time.

How could I fix this?

答案1

得分: 1

通过您的上下文提供程序传递的值是一个具有三个字段的对象:loginlogoutuserToken。因此,当您使用 useContext 读取该值时,您会得到一个对象。

const userToken = useContext(AppContext); // 警告:这里的 userToken 是一个对象

尝试用以下代码替换上面的行:

const appContext = useContext(AppContext);
const userToken = appContext ? appContext.userToken : null

这使用对象解构来从上下文传递的值中提取 userToken 字段。

英文:

The value passed via your context provider is an object with three fields: login, logout and userToken. So, when you read the value using useContext, you are getting an object.

  const userToken = useContext(AppContext); // WARNING: userToken is an object here

Try replacing the previous line with:

  const appContext = useContext(AppContext);
  const userToken = appContext ? appContext.userToken : null

<strike>This uses object destructuring to extract the userToken field form the value passed by the context.</strike>

huangapple
  • 本文由 发表于 2023年1月9日 16:07:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定