react-docgen
react-docgen copied to clipboard
[Typescript] Default props from defaults when destructuring
The defaults work fine from the static .defaultProps
property like ...
interface Props {
foo?: string;
}
const Test: React.SFC<Props> = (props: Props) => {
const { foo, ...rest } = props;
return (
<div { ...rest } />
);
}
Test.defaultProps = {
foo: 'bar',
};
export default Test;
I'm wondering if you think it would be possible to get default prop values from the defaults when destructuring during the render? like ...
interface Props {
foo?: string;
}
const Test: React.SFC<Props> = (props: Props) => {
const { foo = 'bar', ...rest } = props;
return (
<div { ...rest } />
);
};
export default Test;
I guess this could work, also this is not dependent on typescript, this could even be useful with PropTypes
.
I agree, I also see a lot of code where folks destructure the props right in the component function args. In fact, anecdotally, I don't really see defaultProps being used anywhere near as often as setting while destructuring as in the example below. Just want to put my vote in for this.
interface Props {
foo?: string;
}
const Test: React.SFC<Props> = ({ foo = 'bar', ...rest }: Props) => {
return (
<div { ...rest } />
);
};
export default Test;
An alternative way to allow this, but still get default values out would be to take the jsDoc annotation of a prop and parse out the @defalut
as in the example below. Is this an easier way to avoid having to set defalutProps
?
interface Props {
/**
* value for doing foo
* @default 'bar'
*/
foo?: string;
}
const Test: React.SFC<Props> = ({ foo = 'bar', ...rest }: Props) => {
return (
<div { ...rest } />
);
};
export default Test;
Updates on this?