Here's a similar question with an answer: React with TypeScript - define defaultProps in stateless function
import React, { Component } from 'react';
import { Text } from 'react-native';
interface TestProps {
title?: string,
name?: string
}
const defaultProps: TestProps = {
title: 'Mr',
name: 'McGee'
}
const Test: React.SFC<TestProps> = (props) => (
<Text>
{props.title} {props.name}
</Text>
);
Test.defaultProps = defaultProps;
export default Test;
Answer from Matt Stow on Stack OverflowHere's a similar question with an answer: React with TypeScript - define defaultProps in stateless function
import React, { Component } from 'react';
import { Text } from 'react-native';
interface TestProps {
title?: string,
name?: string
}
const defaultProps: TestProps = {
title: 'Mr',
name: 'McGee'
}
const Test: React.SFC<TestProps> = (props) => (
<Text>
{props.title} {props.name}
</Text>
);
Test.defaultProps = defaultProps;
export default Test;
I've found the easiest method is to use optional arguments. Note that defaultProps will eventually be deprecated on functional components.
Example:
interface TestProps {
title?: string;
name?: string;
}
const Test = ({title = 'Mr', name = 'McGee'}: TestProps) => {
return (
<p>
{title} {name}
</p>
);
}
React-Typescript complaining about optional props missing (defined with prop-types)
reactjs - How to pass this TypeScript React component an optional prop? - Stack Overflow
Adding optional props to a React component.
You can simplify it even further by replacing all those props for an optional group prop (and the original name prop), and keeping it to one ternary operator (generally try to avoid nesting them as it’s harder to read)
group ? `icons/${group}/${name}` : `icons/${name}`I’m on mobile hope I formatted this correctly
More on reddit.comOptional types on react component showing undefined as value
Hi, I'm converting a React JS codebase to TS.
I just converted a component (`TabButton) that uses an Icon component that is not converted yet but uses prop-types. The problem is that TS (in TabButton) is complaining that Icon misses some props but they should be completely optional as in Icon I didn't require them.
The error text is: Type '{ component: ReactNode; }' is missing the following properties from type '{ [x: string]: any; component: any; color: any; hoverColor: any; className: any; onClick: any; }': color, hoverColor, className, onClick ts(2739)
What can I do? Thanks
TabButton.tsx Hovering over Icon in TabButton.tsx Icon.jsWhat you're thinking of is declaring an optional property of a type. For instance:
interface Props { bgColor?: string }
const a: Props = { bgColor: '#fff' } // valid
const b: Props = {} // valid
But the only type in your code here is any, which is not a good practice to use as it disables all type checking.
So what you want to do is delcare the type for your props, which includes the optional property:
interface Props {
src: string,
children: React.ReactNode
bgColor?: string
}
Then use that type.
export function BGVideo({ src, children, bgColor }: Props) {
return (
<>...</>
)
}
Now in that function, bgColor has the type string | undefined. A string if it was passed a value, or undefined if it was not passed a value.
Working example
Lastly, React.memo really isn't necesary. You shouldn't really ever need to wrap a function component in this way. React.memo is more for values which you don't want to have to recompute.
The Object where you are trying to add optional prop is a destructured props object and not the type of props object.
You can add type for props object as follows:
export const BGVideo = React.memo(function BGVideo({ src, children, bgColor }: {src: string, children: React.ReactNode, bgColor?: string}) {
return (
<BackgroundVideoContainer>...some other stuff...
)
});
Or better extract the type in a interface or type for better readability:
interface BGVideoProps {
src: string;
children: React.ReactNode;
bgColor?: string;
}
export const BGVideo = React.memo(function BGVideo({ src, children, bgColor }: BGVideoProps) {
return (
<BackgroundVideoContainer>...some other stuff... )
});
Here, bgColor is a optional prop