I tried to use components from the external libraries @mui/material and @mui/labs without success. The following is my code:
import { Box, Tab } from '@mui/material'
import { TabContext, TabList, TabPanel } from '@mui/lab'
import React, { useState } from 'react'
export const MuiTabs = () => {
const [value, setValue] = useState('1')
const handleChanged: Function = (event: React.SyntheticEvent, newValue: string) => {
setValue(newValue)
}
return <Box>
<TabContext value={value}>
<Box sx={{ borderBottom: 1, borderColor: 'divider'}}>
<TabList aria-label='Example Tabs' onChange={handleChanged} textColor='secondary' indicatorColor='secondary' centered>
<Tab label='Tab One' value='1' />
<Tab label='Tab Two' value='2' />
<Tab label='Tab Three' value='3' />
</TabList>
</Box>
<TabPanel value='1'>
Panel one
</TabPanel>
<TabPanel value='2'>
Panel two
</TabPanel>
<TabPanel value='3'>
Panel three
</TabPanel>
</TabContext>
</Box>
}
As I define components from these libraries I’m getting the following error:
No overload matches this call.
Overload 1 of 2, '(props: { component: ElementType<any>; } & BoxOwnProps<Theme> & Omit<any, keyof BoxOwnProps<Theme>>): Element | null', gave the following error.
Type 'Element' is not assignable to type 'ReactNode'.
Overload 2 of 2, '(props: DefaultComponentProps<BoxTypeMap<{}, "div", Theme>>): Element | null', gave the following error.
Type 'Element' is not assignable to type 'ReactNode'.ts(2769)
Box.d.ts(186, 3): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & { component: ElementType<any>; } & BoxOwnProps<Theme> & Omit<any, keyof BoxOwnProps<Theme>>'
Box.d.ts(186, 3): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & BoxOwnProps<Theme> & Omit<Omit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & { ...; }, keyof BoxOwnProps<...>>'
Any idea what could be the issue here?
I have searched online for this problem and couldn’t find anything related to my issue here. I have also tried to use various versions of the mentioned libraries with no success. This is a very simple code snippet and should work, so my guess this has something to do with the installed related packages.
2
Answers
The error you’re encountering seems to be related to the typing mismatch between the components you’re using and their expected types. Specifically, it’s mentioning issues with the
Box
component from@mui/material
.The error message indicates that the
Box
component is receiving a type that is not compatible with what it expects, particularly in terms of thechildren
property.One common reason for such errors could be related to mismatched versions of the libraries or potentially conflicting types from different versions.
Here are a few steps you could take to resolve this issue:
Check Versions: Ensure that you are using compatible versions of
@mui/material
and@mui/lab
. Sometimes, using mismatched versions can lead to these type inference issues.TypeScript Definitions: Ensure that your TypeScript definitions are up-to-date and are compatible with the versions of the MUI (Material-UI) packages you’re using. Sometimes, TypeScript definitions might not align perfectly with the actual implementations.
Library Imports: Make sure you’re importing the components correctly. For instance, check if you’re importing
Box
from@mui/material
andTabContext
,TabList
,TabPanel
from@mui/lab
in the correct way.TypeScript Configuration: Ensure that your TypeScript configuration is set up properly. Sometimes, specific configurations might affect type inference.
Community Forums: Consider checking the GitHub repositories or forums dedicated to
@mui/material
and@mui/lab
for any reported issues or discussions related to the versions or similar typing problems.Lastly, if the issue persists, you might try simplifying the code and testing it in isolation. Sometimes, other parts of the codebase or additional dependencies can inadvertently cause these type inference issues.
If none of these steps resolve the problem, reaching out to the community forums or raising an issue on the GitHub repository of Material-UI might provide more targeted assistance from developers familiar with the libraries.
Try using this (confirmed working):
Note: The function name matches yours
MuiTabs
… the filename is unspecified in the question; in this example, it isDemo.tsx
here … ofc you can change it as needed:And the index.tsx
This is from the official MUI docs here and here