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.

innerRef is leaked as a prop to the wrapped component

See original GitHub issue

Version

1.4.4

Reproduction

https://www.webpackbin.com/bins/-KfSW7ArY2ALUjC5qswH

Steps to reproduce

Wrap any component in styled() and use the innerRef callback to access it.

Expected Behavior

innerRef should not be leaked as a prop to the wrapped child component.

Actual Behavior

innerRef is leaked as a prop to the wrapped child component.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
mxstbrcommented, Apr 1, 2017

I’m not misunderstanding, I get what you’re trying to tell me: Don’t pass innerRef to wrapper child components.

I think what you’re misunderstanding is that this would make some things impossible. Imagine this component:

const Input = ({ className }) => <input type="text" className={className} />

const WrappedInput = styled(Input)``;

Now when I render it, how can I focus the input?

<WrappedInput ref={comp => this.input = comp} />

// this.input is the styled-components wrapper, not the DOM node, so this doesn't work, right?
this.input.focus();

(I don’t know if this is actually the case, but I think it is)

You can fix this with innerRef though:

const Input = ({ className, innerRef }) => <input type="text" ref={innerRef} className={className} />

const WrappedInput = styled(Input)``;
<WrappedInput innerRef={comp => this.input = comp} />

// Works!
this.input.focus();
0reactions
evan-scott-zocdoccommented, Apr 1, 2017

I think you might be misunderstanding what the issue is.

innerRef is an ok pattern to use to access the wrapped child, but in that styled wrapper component it should be deleting the innerRef prop and not forwarding it to the child. That’s what I’m trying to point out. innerRef is a styled-components implementation detail that shouldn’t be leaked.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handle ref props with Higher Order Components - Egghead.io
We will pass the ref as an innerRef prop so that the Higher Order Component can re-assign the ref to the component that...
Read more >
How to Use React Refs - Ross Bulat - Medium
Take a look at the following example, where we pass our forwarded ref down to the wrapped class component as an innerRef prop:...
Read more >
Releases - styled-components
Styled Components Releases. ... Adjust innerRef deprecation warning to not be fired if wrapping a custom component, ... SSR memory leaks? No more!...
Read more >
Memory leaks on React Component - Stack Overflow
I am new to React and am faced with a memory leak issue and I can't figure out ... styles"; function Sidebar(props) {...
Read more >
styled-components | Yarn - Package Manager
Because styled-components allows any kind of prop to be used for styling (a trait shared by most CSS-in-JS libraries, but not the third...
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