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.

Unable to give focus to Form.Input

See original GitHub issue

Hi,

I’m trying to give focus to an <Form.Input> but I get an error : TypeError: this.searchField.current.focus is not a function. Replacing by a standard <input> make it work.

How can I focus an input ?

Here is a sample code :

import React, {Component} from 'react';
import {Form} from 'react-bulma-components'; 

class Home extends Component {
    constructor(props) {
        super(props);
        this.searchField = React.createRef();
        this.focusSearchField = this.focusSearchField.bind(this);
    }

    render() {
        return <div>
            <Form.Control style={{ marginBottom: 20 }}>
                <Form.Input
                    className="input"
                    type="text" 
                    ref={this.searchField}
                    onChange={e => this.changeFilter(e.target.value)} />
            </Form.Control>

             <Button onClick={this.focusSearchField}>Focus</Button>
        </div>
    }

    focusSearchField() {
        this.searchField.current.focus();
    }
}

export default Home;

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
eldyvooncommented, Jul 9, 2019

domRef or ref won’t work with useRef()?

0reactions
gbprodcommented, Apr 16, 2019

That works ! Thanks 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot focus (click into) html input elements - Stack Overflow
The reason is that the .half divs that contain the inputs are floating. Easy fix would be to add style clear:both on you...
Read more >
Cant Focus Input Field - Material Design for Bootstrap
I have the same problem when I form is loaded I want to focus in an input field but the jQuery focus function...
Read more >
Bring Focus to the First Form Field with an Error
Resetting the field will involve removing the aria-invalid attribute and doing one of two things with the error message: either remove it from ......
Read more >
Need help with Failed to set input focus on web page text box.
I have used the web recording function and manually capturing the UI Elements to set focus on a text box, but to no...
Read more >
Unable to programmatically set focus on form input #3270
Hello I need to set a text input field to get focus when I click on an element which contains it. ... I...
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