alisabzevari alisabzevari - 1 year ago 103
React JSX Question

How to define defaultProps for a stateless react component in typescript?

I want to define

for my pure functional component but I get a type error:

export interface PageProps extends React.HTMLProps<HTMLDivElement> {
toolbarItem?: JSX.Element;
title?: string;

const Page = (props: PageProps) => (
<div className="row">
<Paper className="col-xs-12 col-sm-offset-1 col-sm-10" zDepth={1}>
style={{ backgroundColor: "white" }}

Page.defaultProps = {
toolbarItem: null,

I know that I can write this:

(Page as any).defaultProps = {
toolbarItem: null,

Is there a better way to add

Answer Source

You can type Page like this:

const Page: StatelessComponent<PageProps> = (props) => (
    // ...

Then you can write Page.defaultProps without needing to cast to any (the type of defaultProps will be PageProps).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download