Вы можете предоставить значения по умолчанию функционально, составив свой компонент.
interface Props {
someBoolean: boolean;
anotherBoolean: boolean;
children: any;
}
const StackOverflow: React.FC<Props> = (props: Props) => {
return <React.Fragment>{props.children}</React.Fragment>;
};
function withDefaults<T extends React.ComponentType<any>, U extends Partial<React.ComponentProps<T>>>(
Component: T,
defaults: U,
): React.FunctionComponent<Omit<React.ComponentProps<T>, keyof U> & Partial<U>> {
// @see https://github.com/Microsoft/TypeScript/issues/14729
return props => React.createElement(Component, { ...defaults, ...props });
}
Использование:
const StackOverflowWithDefaults = withDefaults(StackOverflow, { someBoolean: false, anotherBoolean: false });
<StackOverflowWithDefaults>
The only prop I need to provide is children
</StackOverflowWithDefaults>