Cześć,
Uczę się reacta i napotkałem na problem. Mianowicie po kliknięciu w button chciałbym odczytać value z inputa.
Robię to w taki sposób:
import React, {Component, Ref} from 'react';
export default class Start extends Component {
private playerOneRef: Ref<HTMLInputElement>;
private playerTwoRef: Ref<HTMLInputElement>;
constructor(props: any) {
super(props);
this.playerOneRef = React.createRef();
this.playerTwoRef = React.createRef();
}
handleClick() {
console.log(this.playerOneRef)
}
render() {
return(
<div>
<input type="text" ref={this.playerOneRef} />
<input type="text" ref={this.playerTwoRef} />
<button onClick={this.handleClick}>
Click
</button>
</div>
);
}
}
Niestety, przy takim podejściu dostaję error że jest undefined (nie to że deklaruję w konstruktorze). Próbowałem jeszcze ustawiać to przez callback, wtedy również dostaję undefined. Jak to poprawnie zrobić w typescript'cie?