GithubHelp home page GithubHelp logo

Comments (6)

joshkel avatar joshkel commented on April 25, 2024 1

@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.

priyanshu-code avatar priyanshu-code commented on April 25, 2024 1

@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.

amirmahdizare avatar amirmahdizare commented on April 25, 2024

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 ;

https://codesandbox.io/embed/react-link-autofocus-forked-lod3su?fontsize=14&hidenavigation=1&theme=dark

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.

joshkel avatar joshkel commented on April 25, 2024

@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.

rothsandro avatar rothsandro commented on April 25, 2024

React DOM handles autoFocus only for buttons, inputs, selects and textareas but not for links. See finalizeInitialChildren and commitMount

from react.

theKashey avatar theKashey commented on April 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.