I'm trying to create a counter that calculates the remaining characters but I cannot. I want to set the allowed characters to 100 and then subtract the number of characters written by the user from the 100, I wrote this
class TextArea extends Component
{
state =
{
chars: 0,
}
charsRemaining()
{
var myTextArea = document.querySelector("textarea").value.length;
this.setState({chars: this.state.chars - myTextArea});
return myTextArea;
}
render()
{
return (
<div>
<textarea onKeyDown={this.charsRemaining()} cols="60" rows="10"></textarea>
<span>{this.state.chars}</span>
</div>
);
};
};
I thought if I set a variable to document.querySelector("textarea").value.length
that would work. But it seems that I cannot access elements DOM this way in ReactJS. Any suggestions?