parent.contains is not a function when used with styled components
See original GitHub issuedownshift
version: 2.0.10node
version: 9.5.0npm
(oryarn
) version: 1.5.1
Relevant code or config
<Downshift
defaultSelectedItem={selectedOption}
itemToString={item => (item ? item.message : '')}
>
{({
highlightedIndex,
itemToString,
getItemProps,
getMenuProps,
getToggleButtonProps,
isOpen,
selectedItem,
toggleMenu,
}) => (
<div>
<Dropdown.Container>
<Button {...getToggleButtonProps()}>Button Toggle</Button>
<Dropdown isOpen={isOpen} {...getMenuProps()}>
{options.map((option, index) => (
<div
{...getItemProps({
key: option.id,
index,
item: option,
isActive: highlightedIndex === index,
})}
/>
))}
</Dropdown>
</Dropdown.Container>
</div>
)}
</Downshift>
What you did: The code above is pseudo code but roughly equates to what I am doing. I am building a dropdown using downshift that toggles when you click a button. I am combining this with styled components and when I click outside of the div I am getting an error from within downshift.
What happened: The error that I am getting is the following:

When digging deeper into this, it’s because the parent in isOrContainsNode is coming through as a Styled Component wrapper as you can see in the screenshot below:

Problem description: Clicking outside of the downshift container div when using styled components is throwing an error and the dropdown window stays open.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:15 (8 by maintainers)
Top Results From Across the Web
parent.contains is not a function when used with styled ...
What you did: The code above is pseudo code but roughly equates to what I am doing. I am building a dropdown using...
Read more >Advanced Usage - styled-components
This function will receive the parent theme, that is from another <ThemeProvider> higher up the tree. This way themes themselves can be made...
Read more >Using 'ref' on React Styled Components is not working
I found the answer myself. The solution is to use innerRef instead of ref as the ref itself points to the Styled Component...
Read more >Complete Guide On How To Use Styled-components In React
It's a component that is styled not by using a CSS file, but by using CSS syntax in JavaScript (components to be precise)....
Read more >Using Styled Components with React Native - Level Up Coding
By passing an interpolated function ${props => props...} to a styled component's template literal you can extend its styles. Now add this button...
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 Free
Top 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
I think the problem is that the
getMenuProps
is trying to set aref
on the<Dropdown />
. Styled components accepts aninnerRef
prop for this, so please try with this insteadAltough we should probably add a check if the
ref
exists before we try accessing it.If nobody is working on this, I’ll take a stab later.