-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
FunctionComponent and ComponentClass are not compatible with LibraryManagedAttributes #87
Comments
Some more context on the issue. I even forgot about it... shame on me. DefinitelyTyped/DefinitelyTyped#30695 |
thanks - i’ll update accordingly. why is it considered a bad idea to have implicitly typed children? i thought that was a major benefit of using React.FC. as a side note - i dont find myself ever using defaultProps for function components as i can assign when i destructure... |
|
update React.FC and defaultProps recommendations with reference to #87
i've put in something quick to reflect this info. It's not clear to me if we should just blanket advise against using React.FC altogether, that feels like a very strong recommendation that we'd have to justify just because of defaultProps. I'm open to it, but simply havent done it yet. Happy to hear your thoughts. |
I wouldn't do that either. I think it's perfectly fine to explain the drawbacks of explicit annotations on function expressions:
function declarations:
Personally I don't like implicit children anyway. Not all of my components handle |
If I could rewrite react types implicit children would be removed. It causes a lot more problems and was only placed there for convenience. I always explicitly include children in my props interfaces anyway when I do support them. |
cool cool i'll incorporate more or less what @eps1lon suggested. makes sense |
@sw-yx Well looks like you're not the only one: Deprecate defaultProps on function components |
That's great to know that React actually think about deprecating defaultProps. Could we come up with a pattern that works and does not use |
i mean.. destructure and assign defaults? const TestFunction: FunctionComponent<Props> = { foo = "bar" } => <div>{foo}</div> |
I was thinking more of something along these lines: const defaultProps = {
foo: 'bar',
};
type Props = typeof defaultProps & {
optional?: string
};
export const TestFunction: FC<Props> = props => {
const { foo } = {
...defaultProps,
...props,
};
return (
<div>{foo}</div>
);
}; As that's the pattern that is currently advised in the doc. |
Default values inside parameters (and when destructured) are better understood by the compiler. It will allow you to not have to hack together the props interface. The compiler knows it will always be defined inside the implementation but optional when consumed. |
Right. |
const TestFunction: FunctionComponent<Props> = ({ foo = "bar" } = {}) => <div>{foo}</div> Also, there isn't really much reason to use |
yep y'all know what i mean |
going to close this now since it seems we havent had much complaints on this front after Seb's initial recommendations |
I'm having trouble finding the alternative to default props for a functional component. In the Readme the link to Martin Hochels article mentions you can use type inference for a class component. But that does not work for an FC component. In an earlier article by the same author (from 2018) he mentions a custom getProps function. That covers all the use-cases as explained in the article, but it seems a bit perculiar. Using prop destructuring gives a linting error (require-default-props). Should this be ignored? Is that the way to go? An example would help. |
The linter error should be ignored or turned off, because destructuring and default values already achieves the same as |
We found a little bit of casting here to be a good compromise. Obviously, if you don't need defaultProps, the best is to use what Sebastian has suggested (we need this to generate docs). import React from 'react';
import PropTypes from 'prop-types';
export type MyComponentProps = {
optionalButDefaulted?: string;
};
const defaultProps = {
optionalButDefaulted: 'optionalButDefaulted',
};
export const MyComponent = (props: MyComponentProps) => {
const { optionalButDefaulted } = props as MyComponentProps &
typeof defaultProps;
return <div>The string length: {optionalButDefaulted.length}</div>;
};
export const myComponentPropTypes: React.WeakValidationMap<MyComponentProps> = {
optionalButDefaulted: PropTypes.string,
};
MyComponent.propTypes = myComponentPropTypes;
MyComponent.defaultProps = defaultProps;
MyComponent.displayName = 'MyComponent'; |
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
Annotating functions and classes with
FunctionComponent
andComponentClass
breaksLibraryManagedAttributes
due to the staticdefaultProps
property on those interfaces being set to optional.This causes
defaultProps
to be completely ignored byJSX.LibraryManagedAttributes
. We should probably remove it as a recommendation from the cheat sheet for now.The text was updated successfully, but these errors were encountered: