Answer the question
In order to leave comments, you need to log in
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>
)
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question