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.

AutoFocus attribute is not triggered on render

See original GitHub issue

Hello,

The autoFocus attribute is not triggered on a render therefor it is impossible to change the focus with that attribute if there are multiple fields.

Currently the only way to change the focus is to give the input a ref and change it on componentDidUpdate()

    componentDidUpdate() {
        if ( this.props.isAutoFocus === true ) {
          this.refs['input' + this.props.line ].focus();
        }
    }


    tempRender = (<li className="content editable" ref={ 'CalculationItem' }>
                    <input
                      ref={ 'input' + this.props.line }
                      autoFocus={ this.props.isAutoFocus }
                    />
                  </li>);

The autoFocus attribute will only work on the initial render.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

15reactions
merlinpattcommented, Mar 30, 2017

I know I’m a couple years late to this discussion but I just found this after experiencing my own similar issues.

I understand that you want it to be in line with spec. That’s fine.

It would be great if you added the option to focus on render (could be just autoFocusOnRender), and just have the docs warn people of the behavior if multiple things call for focus at once. Ideally this wouldn’t happen because an app with good UX would have specific conditions for calling autoFocusOnRender on different inputs.

One particular case that I need this for is that I have a full screen editor with a few inputs. I want it to focus on whichever input is the first to not be filled out. This is simple enough with just plain autoFocus.

The issue is that the full screen editor has multiple pages, each with the same input. When I change the page, the inputs themselves are not re-rendered because they haven’t changed. Thus no focus happens.

I do plan to use a similar solution to the one found above, and it would be great to see if this could be added to React. I’d be happy to do a PR if y’all agree it should be in there.

(Apologies if something like this was already added)

1reaction
zpaocommented, Nov 23, 2015

As mentioned, this seems like it’s in line with the spec. There may be some inconsistencies because I think we fake it. Focus is one of those things that is really tricky to do declaratively because it’s part of a shared global state. If 2 unrelated components declare that they should be focused in a single render pass, who is right? So we give you the hooks to manage that state yourself but we won’t do it for you. Sorry that this has cause you troubles!

Read more comments on GitHub >

github_iconTop Results From Across the Web

React autoFocus attribute is not rendered - Stack Overflow
React will manually call focus() on the relevant element, but it will not add the attribute itself. Here is a quote from Dan...
Read more >
[Solved]-React autoFocus attribute is not rendered-Reactjs
Coding example for the question React autoFocus attribute is not rendered-Reactjs.
Read more >
3 ways to autofocus an input in React that ALMOST always work!
Deep dive into the autofocus attribute, the focus() method, the autoFocus prop, the useRef hook and callback refs.
Read more >
React and autofocus - David Walsh Blog
The autofocus attribute is honored in ReactJS but only when the <input> element is re-rendered with React:
Read more >
4 Ways to Set Focus on an Input Field After Rendering in React
The autofocus attribute of the HTML <input> tag is a boolean attribute. The default value of this is false, but if we mention...
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