typescript - خطای نوع prop sx در مؤلفه TabPanel در MUI v5
من برای اولین بار از MUI با تایپاسکریپت استفاده میکنم و در خصوص پراپ جدید sx با خطا مواجه میشوم. نگاهی به تصویر زیر بیندازید که خطای نمایش داده شده توسط تایپاسکریپت را نشان میدهد.
مسأله خندهدار این است که من فقط در کامپوننت 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 نیست. این یک کامپوننت سفارشی است که شما آن را از مستندات کپی میکنید. (من فرض میکنم از اینجا)
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>();
