Discuss Scratch

banana439monkey
Scratcher
1000+ posts

bit of a funky react question with autofocus

currently adding a “roles” feature to a system that i'm writing. when the user clicks “add role”, the idea is that the input would be autofocused. it is not (at least by default), but when adding the autoFocus attribute to the input, it seems that the user gets scrolled to the top of the page:


how do i prevent this from happening?

Banana

Last edited by banana439monkey (May 14, 2024 17:48:57)



Banana
——————————————————————————–

Very inactive user who has now become a maker.
If you reply to this post it would be greatly appreciated if you could comment the link to your post on my profile!!!

ajskateboarder
Scratcher
1000+ posts

bit of a funky react question with autofocus

I'm not sure how to fix the autoFocus thing, but I think you could workaround this by adding a “useRef” hook to the input and using “whateverRef.current.focus()” on it from the “Add Role” button's onClick handler (also remove the autoFocus attribute from the input)

Last edited by ajskateboarder (May 14, 2024 22:31:42)

banana439monkey
Scratcher
1000+ posts

bit of a funky react question with autofocus

ajskateboarder wrote:

(#2)
I'm not sure how to fix the autoFocus thing, but I think you could workaround this by adding a “useRef” hook to the input and using “whateverRef.current.focus()” on it from the “Add Role” button's onClick handler (also remove the autoFocus attribute from the input)
i mean that fixes the scroll up issue it doesn't seem to autofocus the input now…

Banana


Banana
——————————————————————————–

Very inactive user who has now become a maker.
If you reply to this post it would be greatly appreciated if you could comment the link to your post on my profile!!!

Powered by DjangoBB