Bug: Cursor jumps to the end of input after value refresh
See original GitHub issueI’ve ran into this issue when was trying to implement date input, in which you can adjust date by using arrow up and arrow down keys. When cursor is on month I need to add or subtract one by pressing up and down keys. When cursor is on day, same thing with days. Listed below code should work in theory, but doesn’t behave as expected.
React version: 17.0.0
Steps To Reproduce
- Create controlled input
- Create key press handler, which changes value of input.
Code example:
export function DatePickerTest() {
const [timestamp, setTimestamp] = useState(1609459200000)
const date = new Date(timestamp)
const value = date.getDate() + ""
const onKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
switch (event.code) {
case UP_ARROW_KEY.code:
setTimestamp(prev => prev + 24 * 60 * 60 * 1000)
event.preventDefault() // to prevent default action of moving cursor to start of input
break
case DOWN_ARROW_KEY.code:
setTimestamp(prev => prev - 24 * 60 * 60 * 1000)
event.preventDefault() // to prevent default action of moving cursor to end of input
break
}
}
return el("input", {
value: value,
onKeyDown: onKeyDown,
})
}
The current behavior
When value changes cursor jumps to last position
The expected behavior
Value update should preserve cursor position
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top Results From Across the Web
Cursor jumps to end of controlled input · Issue #955 - GitHub
When an input element is "controlled" by a model, the cursor will jump to the end of the line on every change. This...
Read more >react input cursor moves to the end after update - Stack Overflow
If the cursor jumps to the end of the field it usually means that your component is remounting. It can happen because of...
Read more >Updating input fields without losing the cursor position
When the input field updates as you're typing, the cursor jumps to the end of the input. This is a side-effect of not...
Read more >Possible fix of cursor jumping at the end of the input field
A possible fix is to save and restore the position of the cursor (aka caret) while updating the input field with something like...
Read more >Bugs of Week 11/19/2018 — Jumping Cursor | by Wang Tianjian
In step 3, after 1 second, the store value was changed, and React, noticing that the value is indeed 1234 , decided to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It’s hard to tell without seeing how you use this hook, but generally you need to use
useLayoutEffect
to avoid such issuesHere is a gif of what’s happening, with 30 frps, you can’t always see it 😦