I have a useEffect
useEffect(() => {
const dateOfBirthInput = document.getElementById("date-of-birth-input") as HTMLInputElement
const dataLength = dateOfBirth.length
dateOfBirthInput.selectionStart = dataLength
dateOfBirthInput.selectionEnd = dataLength
}, [dateOfBirth])
Here I’m getting the input with getElementById, then I can’t set selectionStart on the input.
While this code sets selectionStart and selectionEnd
const dateOfBirthOnClick = (e: React.MouseEvent<HTMLInputElement>) => {
const dataLength = dateOfBirth.length;
(e.target as HTMLInputElement).selectionStart = dataLength;
(e.target as HTMLInputElement).selectionEnd = dataLength
}
This code works, the previous doesn’t, is this some kind of "When working with React you should not use Vanilla JS" bug.
Edit:
I’m sorry I added the id to the wrong element. It had to be the input.
<div id="date-of-birth-input">
<input type="text" />
</div>
2
Answers
I can’t find any documentation or reference to either
selectionStart
orselectionEnd
.However, there’s a
setSelectionRange
method:When using react, there are not many cases that require you to directly query the dom. Maybe you wanted to use a ref to your input instead?
EDIT:
The docs for
setSelectionRange
also state this:Ergo, the Selection API is unavailable on
date
inputs for instance.In your useEffect, you’re trying to set the selection on the
dateOfBirthInput
based on thedateOfBirth
value. If thedateOfBirth
value is not updated by the time your useEffect runs, it might lead to unexpected behavior.In other code, in your
dateOfBirthOnClick
function, you’re setting the selection directly based on thee.target
(the input element) without relying on an external value like dateOfBirth. This approach is more reliable because it doesn’t depend on the timing of state updates.Consider using
useRef
too.I believe the following code is a more valid and solid approach to the problem using React :