How to change the cursor position in a textarea with React?

Posted by Editorial Staff | Updated on

Want to change the cursor position in a textarea with React? You have to change the cursor position after the state has been updated(setState() does not immediately mutate this.state).

In order to do that, you have to wrap this.refs.input.selectionStart = this.refs.input.selectionEnd = start + 1; in a function and pass it as the second argument to setState (callback).

Here is jsfiddle example: https://jsfiddle.net/69z2wepo/88019/


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers