Comments (6)
@rothsandro Thank you. That was my conclusion from looking at the React code; however, I think it's incorrect, since autoFocus is a global attribute, and since using button-style links is a common enough web design technique.
from react.
@joshkel I am not an expert into the issue but, I think the issue is with the href attribute, change the "/" to a "#" and the issue is resolved for me : from href="/" to href="#"
I tried making another button without autofocus with the href="/' and the unexpected behavior was still there.
from react.
Hi
This happens because If you render your React component into a detached element, React will call focus() too soon. This will result in the input not focusing when your React tree gets added to the DOM.
Solution
So to achieve this goal you can use the Anchor element Ref and the call the focus() method when the component did mount.
You can see this approach code in my code sandbox ;
I should also emphasize that you can focus on one element (also the first element) in each page load with this approach or autofocus attribute. and will ignore others.
Read more on MDN ;
https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/autofocus
from react.
@amirmahdizare Thank you for your reply, but I don't think it applies in my case. As you can see from my sandbox, I'm not rendering into a detached element. (To demonstrate, modify the sandbox to set autoFocus
on the Button
component, which is rendered at the same time - the button receives the focus as expected.) I'm familiar with using a ref with the focus()
method, but the idea of the autofocus attribute is to avoid having to write imperative code to manipulate the focus like this.
from react.
React DOM handles autoFocus
only for buttons, inputs, selects and textareas but not for links. See finalizeInitialChildren and commitMount
from react.
Related conversation - #11851 (comment)
But still not clear why itβs working for some elements, not for every focusable one
from react.
Related Issues (20)
- Missing Information about useCallback in Custom Hooks Documentation
- [DevTools Bug] Cannot add node "1590" because a node with that id is already in the Store. HOT 8
- Bug: `use` in `lazy` returns content from previous `use` calls on first request
- Bug: `use()` yields incorrect value during first SSR HOT 1
- StrictMode does not support lazily initialized useRef HOT 6
- Bug: Misleading error message when using "use" hook without a Suspense wrapper HOT 1
- Bug: import(metadata.specifier) cause error on windows HOT 1
- Inconsistency Between Server-Side Rendering and Client-Side Rendering Causes "Prop did not match" Error
- Security issue: React is vulnerable to supply chain attacks HOT 12
- [DevTools Bug]: Extension not loading for local files in Electron since 4.27.5 HOT 1
- Bug: RSC render errors aren't caught by Error Boundaries HOT 2
- [DevTools Bug]: React devtool is not there in codesandbox HOT 1
- Feature request: context propagation throughout component tree without Context API or prop drilling (like Vue's provide/inject API) HOT 10
- Bug: Streaming Hydration fails when promise is created within a suspense-wrapped component
- [DevTools Bug]: footer next button hover Ui issues HOT 1
- [DevTools Bug] getCommitTree(): Invalid commit "13" for root "1". There are only "7" commits. HOT 1
- [DevTools Feature Request]: Full support for RSC server elements (component tree, props, inspect)
- [DevTools Bug]: Cannot read properties of undefined (reading 'toLowerCase') HOT 2
- Bug: test HOT 3
- [DevTools Bug]: Extension breaks PRTG web interface HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.