Default property value in React component using TypeScript

前端 未结 7 1046
温柔的废话
温柔的废话 2020-11-28 01:51

I can\'t figure out how to set default property values for my components using Typescript.

This is the source code:

class PageState
{
}

export class         


        
7条回答
  •  北海茫月
    2020-11-28 02:04

    With Typescript 3.0 there is a new solution to this issue:

    export interface Props {
        name: string;
    }
    
    export class Greet extends React.Component {
        render() {
            const { name } = this.props;
            return 
    Hello ${name.toUpperCase()}!
    ; } static defaultProps = { name: "world"}; } // Type-checks! No type assertions needed! let el =

    Note that for this to work you need a newer version of @types/react than 16.4.6. It works with 16.4.11.

提交回复
热议问题