Search code examples
javascriptreactjstypescriptreact-big-calendar

How do I define class methods outside it in typescript reactjs?


I want to use methods for the class as following:

class MyComponent extends React.Component<Props> {
render() {
let { date } = this.props
let range = MyComponent.title(date)

return <TimeGrid {...this.props} range={range} eventOffset={15} />
}
}

My method is:

MyComponent.title = date => {
return `My awesome week: ${date.toLocaleDateString()}`;
};

But I am getting error :

Property 'title' does not exist on type 'typeof MyComponent'


Solution

  • You component will look like this.Add static to your method title.

    class MyComponent extends React.Component<Props> {
          static title=date => {
              return `My awesome week: ${date.toLocaleDateString()}`;
           };
          render() {
              let { date } = this.props
              let range = MyComponent.title(date)
              return <TimeGrid {...this.props} range={range} eventOffset={15} />
          }
    }