Specifying a prop as optional
Sometimes we want a prop to be optional. In this lesson, we'll learn how to do this for React components that have their props strongly-typed with TypeScript.
We'll cover the following
It is possible to do this in TypeScript by putting a ?
before the type annotation.
In our CodeSandbox project that we were working on in the last lesson, add an optional message
prop to the Hello
component. Render the value of the message
prop on the line after the hello message in a p
tag. Also, remove the FC
type and use a type annotation on the props
parameter instead.
Get hands-on with 1400+ tech skills courses.