What is the correct way to define default prop values for functional React components?
Using TypeScript
interface ThingProps {
something?: number;
}
const Thing: FC<ThingProps> = (props: ThingProps): ReactElement => {
const something = props.something || 0
// ...
}
lets me write either
<Thing />
or
<Thing something={someValue} />
both of which work correctly.
Is this the correct idiom, or does React have a different preferred way to achieve this?
Note that while there are answers here about this they are quite old (2016) and none work for me: they result in TS errors with <Thing />
(missing required property) or on uses of something
(could be undefined).