有条件地 Drawer.Navigator 的 openByDefault 属性无法正常工作(“@react-navigation/drawer”:“^5.12.5”)

openByDefault attribute not working properly for Drawer.Navigator conditionally ("@react-navigation/drawer": "^5.12.5")

我正在使用 Drawer.Navigator 在 React Native 中创建一个切换菜单,想要 open/close 基于某些条件的抽屉。它没有正常工作。

<Drawer.Navigator
  drawerContentOptions={{
    activeTintColor: "#e91e63",
    itemStyle: { marginVertical: 5 },
  }}
  drawerContent={(props) => <CustomDrawerContent {...props} />}
  initialRouteName="Home"
  openByDefault={storedCredentials.isDrawerOpen}
>

当我控制 storedCredentials.isDrawerOpen 时,它按预期给出 true 或 false,但它不是 opening/closing 抽屉。 帮助将不胜感激。

更新

    const DrawerRoutes = ({ navigation }) => {
  const { storedCredentials, setStoredCredentials } =
    useContext(CredentialsContext);

  useLayoutEffect(() => {
    storedCredentials.data &&
    storedCredentials.data.flats &&
    storedCredentials.data.flats.length === 1
      ? setStoredCredentials(
          {
            ...storedCredentials,
            flat: storedCredentials.data.flats[0],
            isDrawerOpen: false,
          }
        )
      : setStoredCredentials(
          {
            ...storedCredentials,
            isDrawerOpen: true,
          }
        );
    console.log(storedCredentials);
  }, []);

这是我设置上下文的地方,然后在 openByDefault 属性中使用它。

您可能在 storedCredentials.isDrawerOpen 之后缺少括号 () 像这样使用它 storedCredentials.isDrawerOpen()

openByDefault 无法动态更改。

如果你想open/close手动抽屉,发送相关动作。

https://reactnavigation.org/docs/drawer-actions/