【问题标题】:How do I export and use a function created inside App.js in react native如何在 React Native 中导出和使用在 App.js 中创建的函数
【发布时间】:2020-09-17 23:53:21
【问题描述】:

我创建了一个挂钩来保存用户的身份验证状态。当用户登录后我想更新这个状态,我在 App.js 中创建了一个函数,我如何在我的登录屏幕中使用这个函数来更新钩子,然后将用户发送到主屏幕。

我需要在我的登录屏幕中使用 updateAuth 并将 true 传递给它,它是如何做到的

export default function App() {
  const [isAuthenticated, setIsAuthenticated] = useState(false);
  AsyncStorage.getItem("auth").then((val) =>
    val == "true" ? setIsAuthenticated(true) : setIsAuthenticated(false)
  );

  function updateAuth(value) {
    value ? setIsAuthenticated(value) : null;
  }

    return isAuthenticated ? (
      <NavigationContainer>
        <Stack.Navigator headerMode="none" initialRouteName="Home">
          <Stack.Screen name="Home" component={Home} />
          <Stack.Screen name="Product" component={Product} />
          <Stack.Screen name="Orders" component={Orders} />
        </Stack.Navigator>
      </NavigationContainer>
    ) : (
      <NavigationContainer>
        <Stack.Navigator headerMode="none">
          <Stack.Screen name="Welcome" component={Welcome} />
          <Stack.Screen name="Login" component={Login} />
          <Stack.Screen name="Signup" component={Signup} />
        </Stack.Navigator>
      </NavigationContainer>
    );
  1. 列表项

    }

【问题讨论】:

    标签: reactjs react-native react-navigation


    【解决方案1】:

    使用initialParams 属性。

    Documentation

    <Stack.Screen name="Login" component={Login} initalParams={{isAuthenticated}} />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      • 2020-09-23
      • 2019-08-01
      • 2020-10-07
      相关资源
      最近更新 更多