A
A
aleks_web_dev2020-09-02 16:58:22
typescript
aleks_web_dev, 2020-09-02 16:58:22

How to make a ref using typescript?

import React, { Component } from 'react';


type TypeProps={
  title?:string
}
type typeState={
  counter:number
  focus:boolean
}

export default class App extends Component<TypeProps,typeState> {
  constructor(props:TypeProps){
    super(props)
    this.state={
      counter:0,
      focus:false
    }
    this.myRef = React.createRef();
  }

  componentDidMount():void{
    console.log(this.refs); 
    
  }

  handlerPlusCounter=()=>{
    this.setState(prevState=>{
      return {counter:prevState.counter+1}
    })
  }
  

  render() {
    return (
      <div>
        <input type="text" ref={this.myRef} />
        <h1>{this.state.counter}  </h1>
 
        <button onClick={this.handlerPlusCounter}>+</button>
        <h3>{this.props.title} </h3>
      </div>
    )
  }
}


Property 'myRef' does not exist on type 'App'.
Property 'myRef' does not exist on type 'App'.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eduard07, 2020-09-02
@Eduard07

const groupEditElemets = useRef<(HTMLDivElement)[]>([])
For example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question