Are there any significant differences between those two ways to define props in React.FunctionalComponent
?
interface Props {
username: string,
age: number
}
const UserPanel: React.FunctionComponent = (props: Props) => (
<div>{props.username}</div>
);
const UserPanel: React.FunctionComponent<Props> = (props) => (
<div>{props.age}</div>
);
2
Answers
Yes, a quite significant difference: Option one is throwing away type information.
The function on the right hand side includes information about the props, but then you assign it to a variable that does not have that information.
UserPanel
‘s type uses the default props of{}
so whoever uses UserPanel can not pass in any props. Attempting to pass in a username prop or age prop will throw an error. For example:Playground link
You either need to do your second option:
Or you need to do this:
The first ways to define props in
React.functionComponent
will emit an error:If we look into the type definition of
React.FunctionComponent
(in my case React v18.2.45) we’ll see:Basically, the error tells us that the definition of the
props
inReact.FunctionComponent
{}
does not match the definition ofinterface Props { name: string, age: number }
which you passed in.Let’s explain the type definition of
props
inReact.FunctionComponent
.P = {}
: This is a generic parameter. It represents the props that the function component expects. By default, it’s an empty object, meaning no props are expected.(props: P, context?: any): ReactNode
: This is the function signature for the component. It takes two parameters: props and context. props is of typeP
(the props the component expects), and context is optional and can be of any type. The function returns a ReactNode, which can be any valid React child (a React element, string, number, etc.).Because in
React.FunctionComponent
,props
is expecting the generic typeP
(which is{}
), you have to define the generic typeP
inorder to pass intoUserPanel
component. This is where your second way to define props inReact.FunctionComponent
comes in.