i'm setting a textarea's selectionRange using a react ref, but that contradicts with the react "way" .
the ref is working perfectly .
i want to set the textarea's selectionRange using state, not a ref ( like in this code snippet )
import React, { Component } from 'react';
export default class App extends Component {
constructor() {
super();
this.state = {
value:"some placeholder text",
}
this.textareaRef = React.createRef();
}
handleChange=({ target : { value : text } })=>{
this.setState({value : text});
};
componentDidMount(){
// setting the cursor position to the end of text on mount .
const textareaObj = textareaRef.current;
const cursor_pos = this.state.value.length;
textareaObj.setSelectionRange(cursor_pos , cursor_pos );
textareaObj.focus();
}
render(){
return (
<textarea
value={this.state.value}
onChange={this.handleChange}
ref={textareaRef}>
</textarea>
);
}
}