-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[material-ui][docs] Document the correct way to extend a component #19461
Comments
Can you try |
@embeddedt That causes the same typescript issue |
Related: #17491. |
Until that issue is resolved, you can pass along the generic parameters: import Link, { LinkProps, LinkTypeMap } from "@material-ui/core/Link";
import React from "react";
export const HnLink = <
D extends React.ElementType = LinkTypeMap["defaultComponent"],
P = {}
>(
props: LinkProps<D, P>,
) => {
return <Link {...props} />;
};
export const test = <HnLink to="/test" />; You can retrieve them by viewing the type of export type LinkProps<
D extends React.ElementType = LinkTypeMap['defaultComponent'],
P = {}
> = OverrideProps<LinkTypeMap<P, D>, D>; |
The shorter example of implementing The problem with implementing Say we have following fictional type
Then we want to use it like
We expect TS to infer the generic type So I don't see a way to implement |
@strothj Thanks for this solution. But how i can overwrite prop type? Lets say, i want to do something like this: import {Button as MUIButton, ButtonProps, ButtonTypeMap} from '@material-ui/core';
import * as React from 'react';
export const Button = <
D extends React.ElementType = ButtonTypeMap['defaultComponent'],
P = {}
>(props: { color?: string } & Omit<ButtonProps<D, P>, 'color'>) => {
const {color, ...restProps} = props;
return (
<MUIButton {...restProps} />
);
}; Its throw an error
|
and forwardRef? |
So how is the state of this in 2021 ? I am getting the following error:
with the following code: import { Button as MuiButton, ButtonProps } from "@mui/material";
import { FC } from "react";
const Button: FC<ButtonProps> = ({ text, variant }) => {
return (
<MuiButton variant={variant} color="inherit">
{text}
</MuiButton>
);
};
export default Button; |
import { Button as MuiButton, ButtonProps } from "@mui/material";
import { FC } from "react";
// Add your custom props here
type MyButtonProps = {
text: string
}
const Button: FC<ButtonProps & MyButtonProps > = ({ text, variant }) => {
return (
<MuiButton variant={variant} color="inherit">
{text}
</MuiButton>
);
};
export default Button; |
We are using Material-UI as the base of a project, but are running in to issues extending components
Works fine
Does not accept
component
andto
props, resulting in TypeScript errors.The same is true of Tabs, wherein wrapping Tabs in our own component causes onChange to not be accepted
As also seen here: #17454
Is there a 'correct' way to wrap a Material-UI component that can alleviate typing issues?
The text was updated successfully, but these errors were encountered: