React: handling an input caret position

Alberto De Natale
3 min readDec 13, 2020

When we modify the text of an HTML input while the user types using React, we may see the caret jumping at the end of the text.

This happens typically when we modify the length of the text the user is typing.

In the example below, a React component is stripping away the ‘&’ illegal characters from the string ‘asd&fasfe’:

--

--

Alberto De Natale

Alberto De Natale is a passionate tech-enthusiast software developer.