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.

parent.contains is not a function when used with styled components

See original GitHub issue
  • downshift version: 2.0.10
  • node version: 9.5.0
  • npm (or yarn) 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:

screen shot 2018-06-27 at 14 40 47

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:

screen shot 2018-06-27 at 14 45 08

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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
alexandernanbergcommented, Jun 27, 2018

I think the problem is that the getMenuProps is trying to set a ref on the <Dropdown />. Styled components accepts an innerRef prop for this, so please try with this instead

<Dropdown isOpen={isOpen} {...getMenuProps({ refKey: 'innerRef' })} />

Altough we should probably add a check if the ref exists before we try accessing it.

4reactions
franklixuefeicommented, Jun 29, 2018

If nobody is working on this, I’ll take a stab later.

On Jun 29, 2018, at 6:23 AM, Kent C. Dodds notifications@github.com wrote:

And we should add type definitions.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

Read more comments on GitHub >

github_iconTop 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 >

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