不要在渲染过程中定义组件,React将在每次渲染时看到一个新的组件类型,并销毁整个子树的DOM节点和状态

q1qsirdb  于 2023-03-13  发布在  React
关注(0)|答案(1)|浏览(80)

在下面的代码中,我在第1行得到的,有人能告诉我解释和我应该改变什么吗

<Drawer.Navigator
  screenOptions={{
    headerStyle: { backgroundColor: colorPalette.blue1, height: 80 },
    headerTintColor: "white",
    // headerLeft: () => <View></View>
    headerTitle: () => <Image source={appLogo} />, // line 1
    headerRight: () => <HeaderRight />,
  }}
  drawerContent={(props) => (
    <DrawerContent navigation={navigation} {...props}></DrawerContent>
  )}
>

我想用它来渲染一个组件?

6kkfgxo0

6kkfgxo01#

我解决了在其父组件(DrawerMenuNavigator)之外声明<CustomDrawerContent />组件的相同问题,如下所示:

function CustomDrawerContent(props: DrawerContentComponentProps) {
      return (
        <DrawerContentScrollView {...props}>
          <DrawerItemList {...props} />
        </DrawerContentScrollView>
      );
    }
    
    const Drawer = createDrawerNavigator<DrawerNavigationProps>();
    
    const DrawerMenuNavigator = ({}) => {
      return (
        <Drawer.Navigator
          drawerContent={CustomDrawerContent}
          initialRouteName={DRAWER_MENU_NAVIGATOR.DASHBOARD_NAVIGATOR}
          screenOptions={{headerShown: false, drawerStyle: styles.drawer}}>
          <Drawer.Screen
            component={DashboardNavigator}
            name={DRAWER_MENU_NAVIGATOR.DASHBOARD_NAVIGATOR}
          />
        </Drawer.Navigator>
      );
    };

相关问题