admin管理员组

文章数量:1431918

Error:

Property 'ref' does not exist on type 'A'.ts(2339)

Here is my source code

export default class A extends React.PureComponent<Props, State> {
  constructor(props: Props) {
    super(props);

    this.ref = React.createRef(); << ERROR HERE

How can I define type of A to resolve the error?

Error:

Property 'ref' does not exist on type 'A'.ts(2339)

Here is my source code

export default class A extends React.PureComponent<Props, State> {
  constructor(props: Props) {
    super(props);

    this.ref = React.createRef(); << ERROR HERE

How can I define type of A to resolve the error?

Share asked Aug 7, 2020 at 17:29 merry-go-roundmerry-go-round 4,63513 gold badges59 silver badges112 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You have to declare the ref variable in the class body.

export default class A extends React.PureComponent<Props, State> {
   ref: React.RefObject<HTMLInputElement>;

   constructor(props: Props) {
      super(props);

      this.ref = React.createRef();
   }
}

or basically remove the constructor and move logic outside.

export default class A extends React.PureComponent<Props, State> {
  ref = React.createRef();

本文标签: javascriptProperty 39ref39 does not exist on type 39A39ts(2339)ReacttypescriptStack Overflow