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 doesnt register if you dynamically create the element

See original GitHub issue

Version

2

Reproduction

const DynamicComponent = styled(({ children, tag, ...props }) => {
    return React.createElement(tag, props, children);
})`
// Some css
`;

// Usage
<DynamicComponent tag='div' innerRef={ref => this.someRef = ref} />

Steps to reproduce

Use React.createElement to return an element to styled and try to use innerRef

Expected Behavior

innerRef should be assigned to the returned React element

Actual Behavior

ref is null

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
cusxiocommented, Jul 20, 2017

@cjthedizzy, @philpl

Since this pattern is fairly common in styled-components, is there any work around to get the “ref” of the component?

1reaction
kittencommented, Apr 25, 2017

There’s a couple of things here actually:

  • I’m not sure which version you’re using. Are you on v2.0.0-15?
  • innerRef is turned into ref for targets that aren’t styled components. Since you’re passing in an anonymous functional component, the ref will point to it
  • The problem however is that refs to functional components are not supported in React

I hope this helps you resolve this 😃

You can actually also look at our tests here, if it’s still unclear: https://github.com/styled-components/styled-components/blob/739e1a003cfd51f0dc07a64c01de0d8d93a3c945/src/test/basic.test.js#L61

Read more comments on GitHub >

github_iconTop Results From Across the Web

Refs for dynamically generated components in React?
This code generates 3 Note components using Array.map and when you hit enter in them it empties the statically generated Note component above ......
Read more >
How to use React's forwardRef function | Felix Gerschau
Let's say we want to programmatically focus an input element when the user clicks a button. React doesn't have a built-in way of...
Read more >
FAQs - styled-components
If you were using the innerRef prop, change it to a normal ref.. const Component = styled.div`.
Read more >
Multi List Drag and Drop With react-beautiful-dnd, Immer, and ...
We 'll explore how to use react-beautiful-dnd to create multi-list drag ... then some dynamic classnames to apply when dragging an item/dragging over...
Read more >
Link and NavLink components in React-Router-Dom
to, replace, innerRef same as the Link Component. className: Specifies the CSS class name you want to apply to the element when active....
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