gpt4 book ai didi

typescript - TabPanel 组件中的 MUI v5 sx Prop 类型错误

转载 作者:行者123 更新时间:2023-12-04 14:47:40 27 4
gpt4 key购买 nike

我第一次在 TypeScript 中使用 MUI,但在新的 sx 上出现错误支柱。请看下面有关 Typescript 显示的错误的屏幕截图。
enter image description here
有趣的是,我只在 TabPanel 上收到此错误组件,因为我已经在其他 MUI 组件上使用它没有问题。
下面是我的代码

/* eslint-disable react/require-default-props */
import * as React from 'react';
import Tabs from '@mui/material/Tabs';
import Tab from '@mui/material/Tab';
import Typography from '@mui/material/Typography';
import Box from '@mui/material/Box';

interface TabPanelProps {
children?: React.ReactNode;
index: number;
value: number;
}

function TabPanel(props: TabPanelProps) {
const { children, value, index, ...other } = props;

return (
<div
role="tabpanel"
hidden={value !== index}
id={`simple-tabpanel-${index}`}
aria-labelledby={`simple-tab-${index}`}
{...other}
>
{value === index && (
<Box sx={{ p: 3 }}>
<Typography>{children}</Typography>
</Box>
)}
</div>
);
}

function a11yProps(index: number) {
return {
id: `simple-tab-${index}`,
'aria-controls': `simple-tabpanel-${index}`
};
}

export default function MenteeVolunteerTabs() {
const [value, setValue] = React.useState(0);

const handleChange = (event: React.SyntheticEvent, newValue: number) => {
setValue(newValue);
};

return (
<Box sx={{ width: '100%' }}>
<Box sx={{ borderBottom: 1, borderColor: 'divider' }}>
<Tabs value={value} onChange={handleChange} aria-label="basic tabs example">
<Tab label="Volunteer" {...a11yProps(0)} />
<Tab label="Mentee" {...a11yProps(1)} />
</Tabs>
</Box>
<TabPanel value={value} index={0} sx={{ backgroundColor: '#F4F6F7' }}>
Item One
</TabPanel>
<TabPanel value={value} index={1}>
Item Two
</TabPanel>
</Box>
);
}
请帮助解决此错误

最佳答案

TabPanel不是 MUI 组件。这是您从文档中复制的自定义组件。 (我假设来自 here )

function TabPanel(props) {
const { children, value, index, ...other } = props;

return (
<div
role="tabpanel"
hidden={value !== index}
id={`simple-tabpanel-${index}`}
aria-labelledby={`simple-tab-${index}`}
{...other}
>
{value === index && (
<Box sx={{ p: 3 }}>
<Typography>{children}</Typography>
</Box>
)}
</div>
);
}
添加 sx支持您的自定义组件,您可以使用 styled() :
type TabPanelProps = {
children: React.ReactNode;
index: number;
value: number;
};

const TabPanel = styled((props: TabPanelProps) => {
const { children, value, index, ...other } = props;

return (
<div
role="tabpanel"
hidden={value !== index}
id={`simple-tabpanel-${index}`}
aria-labelledby={`simple-tab-${index}`}
{...other}
>
{value === index && (
<Box sx={{ p: 3 }}>
<Typography>{children}</Typography>
</Box>
)}
</div>
);
})<TabPanelProps>();
Codesandbox Demo

关于typescript - TabPanel 组件中的 MUI v5 sx Prop 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69726025/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com