Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » bit of a funky react question with autofocus
- banana439monkey
-
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

how do i prevent this from happening?
Banana
Last edited by banana439monkey (May 14, 2024 17:48:57)
- ajskateboarder
-
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
-
1000+ posts
bit of a funky react question with autofocus
(#2)i mean that fixes the scroll up issue it doesn't seem to autofocus the input now…
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)
Banana
- Discussion Forums
- » Advanced Topics
-
» bit of a funky react question with autofocus