[Question] How to use "ref" to access the DOM when using styled.
See original GitHub issueI 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:
- Created 6 years ago
- Comments:6 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You hav to use
innerRef
insteadref
I have changed to
and
And I still have not access to my element :
@k15a Why is that?