英文:
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 = () => {
const drawerWidth = "40%";
const [showViewController, setShowViewController] = useState(false);
const handleOpenViewController = () => setShowViewController(true)
const handleCloseViewController = () => setShowViewController(false)
return (
<>
// More components here...
<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>
</>
)
}
My DrawerForm component:
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>
);
};
答案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 = "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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论