Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'ref' does not exist on type 'A'.ts(2339) - React, TypeScript

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?

like image 598
merry-go-round Avatar asked Aug 03 '26 14:08

merry-go-round


1 Answers

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();
like image 150
kind user Avatar answered Aug 06 '26 01:08

kind user



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!