【问题标题】:How to update use context variable from child component in react native如何在本机反应中更新子组件的使用上下文变量
【发布时间】:2021-08-21 15:01:37
【问题描述】:

我正在使用 useMemo 和 useContext 挂钩在我的 react 本机应用程序中存储登录和注销状态。

import { AuthContext } from "./src/app/context";



export default () => {
  const [isLoading, setIsLoading] = React.useState(true);
  const [userToken, setUserToken] = React.useState(null);
  const [userType, setUserType] = React.useState(null);
  const [fontsLoaded, setFontsLoaded] = React.useState(false);
  
  const authContext = React.useMemo(() => {
    return {
      signIn: (userType) => {
        setUserToken("dummy");
        setUserType(userType);
      },
      signUp: () => {
        setIsLoading(false);
      },
      signOut: () => {
        setIsLoading(false);
        setUserToken(null);
      },
      showBottomNavigation: false
    };
  }, []);

  React.useEffect(() => {
    setTimeout(() => {
      setIsLoading(false);
    }, 1000);
  }, []);

  return (
    <AuthContext.Provider value={authContext}>
      <NavigationContainer>
        <RootStackScreen userToken={userToken} userType={userType}/>
      </NavigationContainer>
    </AuthContext.Provider>
  );
};

上下文 js 文件如下所示:

import React from "react";

export const AuthContext = React.createContext();

我想从子组件更新 showBottomNavigation(在 useMemo 对象中)。

我是这些概念的新手,任何建议如何更新 showBottomNavigation 并使用它。

【问题讨论】:

    标签: reactjs react-native react-router react-hooks react-navigation-bottom-tab


    【解决方案1】:

    您可以使用状态来更改值。您的子组件可以更改状态,进而可以更改上下文的值。

    import { AuthContext } from "./src/app/context";
    
    
    
    export default () => {
      const [isLoading, setIsLoading] = React.useState(true);
      const [userToken, setUserToken] = React.useState(null);
      const [userType, setUserType] = React.useState(null);
      const [fontsLoaded, setFontsLoaded] = React.useState(false);
    
      const [bottomNavigation, setBottomNavigation] = useState(false);
      
      
      const authContext = React.useMemo(() => {
        return {
          signIn: (userType) => {
            setUserToken("dummy");
            setUserType(userType);
          },
          signUp: () => {
            setIsLoading(false);
          },
          signOut: () => {
            setIsLoading(false);
            setUserToken(null);
          },
          showBottomNavigation: false
        };
      }, []);
    
      React.useEffect(() => {
        setTimeout(() => {
          setIsLoading(false);
        }, 1000);
      }, []);
    
      return (
        <AuthContext.Provider value={...authContext,showBottomNavigation:bottomNavigation }>
          <NavigationContainer>
            <RootStackScreen userToken={userToken} userType={userType}/>
            <Text onPress={()=>setBottomNavigation(true)}>Change Bottom Nav</Text>
          </NavigationContainer>
        </AuthContext.Provider>
      );
    };
    

    【讨论】:

    • OnPress 在文本元素上我们正在更改状态信息,我们如何从子组件更新?
    • 文本元素是子组件。您可以将其替换为任意组件并将其传递给setBottomNavigation。然后该组件可以随时更改状态。
    猜你喜欢
    • 2023-01-04
    • 2019-05-14
    • 2022-07-22
    • 2021-08-07
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    相关资源
    最近更新 更多