简体   繁体   中英

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?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM