【问题标题】:openByDefault attribute not working properly for Drawer.Navigator conditionally ("@react-navigation/drawer": "^5.12.5")有条件的 Drawer.Navigator 的 openByDefault 属性无法正常工作(“@react-navigation/drawer”:“^5.12.5”)
【发布时间】:2021-10-22 16:18:18
【问题描述】:

我正在使用 Drawer.Navigator 在 react native 中创建一个切换菜单,希望根据某些条件打开/关闭抽屉。它不能正常工作。

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

当我控制storedCredentials.isDrawerOpen 时,它按预期给出true 或false,但它没有打开/关闭抽屉。 我们将不胜感激。

更新

    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 属性中使用它。

【问题讨论】:

    标签: react-native react-navigation-v5


    【解决方案1】:

    openByDefault 不能动态更改。

    如果您想手动打开/关闭抽屉,请发送相关操作。

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

    【讨论】:

    • 谢谢,我已经想通了,但是这行得通,我需要使用 navigation.dispatch 操作而不是 openByDefult。
    【解决方案2】:

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

    【讨论】:

    • storedCredentials.isDrawerOpen 是一个对象,而不是我从上下文中获取的方法/函数。我已经更新了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多