我的子组件未在我的抽屉组件内呈现 – Material UI,React

huangapple go评论59阅读模式
英文:

My childrens components are not rendered inside my Drawer component - Material UI, React

问题

当我调用我的DrawerForm(使用Material UI的抽屉组件)并添加Material UI组件以及项目中存在的其他组件时,为什么它们不会显示在我的DrawerForm中?

我的依赖项:

  • react: 18.2.0
  • react-dom: 18.2.0
  • @amcharts/amcharts5: 5.3.6
  • @mui/icons-material: 5.11.11
  • @mui/material: 5.11.12

调用和渲染组件的index.js

export const CustomMap = () => {
  const drawerWidth = "40%";
  const [showViewController, setShowViewController] = useState(false);

  const handleOpenViewController = () => setShowViewController(true);
  const handleCloseViewController = () => setShowViewController(false);

  return (
    <>
      // 更多组件在这里...
      <FabForm
        position={["absolute"]}
        top="16px"
        icon="arrowLeft"
        handleClick={handleOpenViewController}
      />
      <DrawerForm
        drawerWidth={drawerWidth}
        variant="persistent"
        open={showViewController}
        close={handleCloseViewController}
      >
        <Box
          sx={{
            position: "relative",
            top: "130px",
          }}
        >
          <DateChart divID="chart1" />
          <DateChart divID="chart2" />
          <DateChart divID="chart3" />
        </Box>
      </DrawerForm>
    </>
  );
}

我的DrawerForm组件:

export const DrawerForm = ({
  drawerWidth,
  variant,
  anchor = "right",
  open,
  close,
  color = "white",
}) => {
  return (
    <Drawer
      sx={{
        width: drawerWidth,
        flexShrink: 0,
        "& .MuiPaper-root": {
          width: drawerWidth,
          backgroundColor: color,
        },
      }}
      variant={variant}
      anchor={anchor}
      open={open}
    >
      <FabForm
        top="75px"
        icon="arrowRight"
        handleClick={close}
      />
    </Drawer>
  );
};
英文:

Why when I invoke my DrawerForm(Drawer Material UI) component and add Material UI components as well as other components that exist in the project are not displayed in my DrawerForm?

My dependencies:

  • react: 18.2.0
  • react-dom: 18.2.0
  • @amcharts/amcharts5: 5.3.6
  • @mui/icons-material: 5.11.11
  • @mui/material: 5.11.12

My index.js that invoke and render the components:

export const CustomMap = () =&gt; {
  const drawerWidth = &quot;40%&quot;;
  const [showViewController, setShowViewController] = useState(false);

  const handleOpenViewController = () =&gt; setShowViewController(true)
  const handleCloseViewController = () =&gt; setShowViewController(false)

  return (
   &lt;&gt;
    // More components here...
    &lt;FabForm
      position={[&quot;absolute&quot;]}
      top=&quot;16px&quot;
      icon=&quot;arrowLeft&quot;
      handleClick={handleOpenViewController}
    /&gt;
    &lt;DrawerForm
      drawerWidth={drawerWidth}
      variant={&quot;persistent&quot;}
      open={showViewController}
      close={handleCloseViewController}
    &gt;
      &lt;Box
        sx={{
          position: &quot;relative&quot;,
          top: &quot;130px&quot;,
        }}
      &gt;
        &lt;DateChart divID=&quot;chart1&quot; /&gt;
        &lt;DateChart divID=&quot;chart2&quot; /&gt;
        &lt;DateChart divID=&quot;chart3&quot; /&gt;
      &lt;/Box&gt;
    &lt;/DrawerForm&gt;
   &lt;/&gt;
  )
}

My DrawerForm component:

export const DrawerForm = ({
  drawerWidth,
  variant,
  anchor = &quot;right&quot;,
  open,
  close,
  color = &quot;white&quot;,
}) =&gt; {
  return (
    &lt;Drawer
      sx={{
        width: drawerWidth,
        flexShrink: 0,
        &quot;&amp; .MuiPaper-root&quot;: {
          width: drawerWidth,
          backgroundColor: color,
        },
      }}
      variant={variant}
      anchor={anchor}
      open={open}
    &gt;
      &lt;FabForm
        top=&quot;75px&quot;
        icon=&quot;arrowRight&quot;
        handleClick={close}
      /&gt;
    &lt;/Drawer&gt;
  );
};

答案1

得分: 0

You need to pass the children prop. You should study how to handle props better. Here's the documentation on how to handle children: https://react.dev/learn/passing-props-to-a-component#passing-jsx-as-children.

What you're looking for is probably one of these:

export const DrawerForm = ({
  drawerWidth,
  variant,
  anchor = "right",
  open,
  close,
  color = "white",
  children // this prop
}) => {
  return (
    <Drawer
      sx={{
        width: drawerWidth,
        flexShrink: 0,
        "& .MuiPaper-root": {
          width: drawerWidth,
          backgroundColor: color,
        },
      }}
      variant={variant}
      anchor={anchor}
      open={open}
    >
      <FabForm
        top="75px"
        icon="arrowRight"
        handleClick={close}    
      >
        {children} {/* <- add children either here */}
      </FabForm>
      {children} {/* <- or here */}
    </Drawer>
  );
};

I'm not sure if you want it to be inside FabForm or inside Drawer. That's why I answered with both.

英文:

You need to pass the children prop. You should study how to handle props better. Here's the documentation on how to handle children: https://react.dev/learn/passing-props-to-a-component#passing-jsx-as-children.

What you're looking for is probably one of these:

export const DrawerForm = ({
drawerWidth,
variant,
anchor = &quot;right&quot;,
open,
close,
color = &quot;white&quot;,
children // this prop
}) =&gt; {
return (
&lt;Drawer
sx={{
width: drawerWidth,
flexShrink: 0,
&quot;&amp; .MuiPaper-root&quot;: {
width: drawerWidth,
backgroundColor: color,
},
}}
variant={variant}
anchor={anchor}
open={open}
&gt;
&lt;FabForm
top=&quot;75px&quot;
icon=&quot;arrowRight&quot;
handleClick={close}    
&gt;
{children} {/* &lt;- add children either here */}
&lt;/FabForm&gt;
{children} {/* &lt;- or here */}
&lt;/Drawer&gt;
);
};

I'm not sure if you want it to be inside FabForm or inside Drawer. That's why I answered with both.

huangapple
  • 本文由 发表于 2023年3月31日 23:30:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定