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.

[Question] How to use "ref" to access the DOM when using styled.

See original GitHub issue

I have a case where I click on a toggle <Button />, then I need to focus the <Input />.

The <Input /> is a component made with styled-component v2.

I have the following code in the Component :


  componentDidUpdate() {
    if (this.search) {
      this.search.getDOMNode().focus();
    }
  }

  render() {
    return (
      <div className="input-group">
        <div className="input-group-add-on"><Fa search /></div>
        <Input ref={(search) => { this.search = search }} type="text" name="search" placeholder="$btn-primary"/>
      </div>
    )
  }

I have the following error:

 this.search.getDOMNode is not a function

After some inspection, it appear I can’t use this with styled. What is the recommended way to deal with this issue ?

Version

2.0.0-8

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

186reactions
k15acommented, Mar 24, 2017

You hav to use innerRef instead ref

14reactions
kopaxcommented, Mar 24, 2017

I have changed to

  componentDidUpdate() {
    if (this.search) {
      this.search.focus();
    }
  }

and

   <Input innerRef={(search) => { this.search = search }} type="text" name="search" placeholder="$btn-primary"/>

And I still have not access to my element :

 this.search.focus is not a function

@k15a Why is that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Styled components: cannot find name 'ref' when trying to pass ...
What are you trying to do? Why would a component need to access its own DOM node to determine its styling? I doubt...
Read more >
Manipulating the DOM with Refs - React Docs
To access a DOM node managed by React, first, import the useRef Hook: ... The useRef Hook returns an object with a single...
Read more >
A complete guide to React refs - LogRocket Blog
Learn how to use React refs, and why it's important to use them only when React can't handle a function call through its...
Read more >
Manipulating documents - Learn web development | MDN
This is usually done by using the Document Object Model (DOM), a set of APIs for controlling HTML and styling information that makes...
Read more >
How to access DOM Nodes using refs in React - Educative.io
In React, Ref is a way we can get hold of DOM nodes in JSX in the render method. Without this, we use...
Read more >

github_iconTop Related Medium Post

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