innerRef doesnt register if you dynamically create the element
See original GitHub issueVersion
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:
- Created 6 years ago
- Comments:8 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
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
@cjthedizzy, @philpl
Since this pattern is fairly common in
styled-components
, is there any work around to get the “ref” of the component?There’s a couple of things here actually:
innerRef
is turned intoref
for targets that aren’t styled components. Since you’re passing in an anonymous functional component, the ref will point to itI 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