question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: Cursor jumps to the end of input after value refresh

See original GitHub issue

I’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

  1. Create controlled input
  2. 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:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
vkurchatkincommented, Mar 24, 2021

It’s hard to tell without seeing how you use this hook, but generally you need to use useLayoutEffect to avoid such issues

1reaction
IlyaHalskycommented, Mar 24, 2021

Here is a gif of what’s happening, with 30 frps, you can’t always see it 😦 cursor

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found