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.

Filter props which are passed to underlying component

See original GitHub issue

As written in the documentation non-html-standard props are filtered out. But this seems to be only the case for styled HTML-elements not for styled components. Which is okay if I style my own components and thus have control which props are getting passed along to the DOM. But if I do not, I will get an error. In my case, I have a wrapper for the react-bootstrap Component Panel, which should just serve as an example of a component that I do not ‘control’.

Environment

System:

  • OS: Windows 10
  • CPU: (8) x64 Intel® Core™ i7-3632QM CPU @ 2.20GHz
  • Memory: 2.21 GB / 15.88 GB

Binaries:

  • Node: 10.15.1 - C:\Program Files\nodejs\node.EXE
  • npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD

Reproduction

// ...
   import RbPanel from 'react-bootstrap/lib/Panel';
// .. .

const Panel = styled(RbPanel)`
        ${(props) => props.withBodyWrapper ? 'padding: 0' : 'padding: 15px'};
    `;
    
class Panel extends React.Component {
    return (
            <Panel withBodyWrapper={true}>
                TEST
            </Panel>
    );
}

Steps to reproduce

see code above

Expected Behavior

have some option to filter out props that I just want to pass to the styled component but not the underlying custom component (which I do not control)

Actual Behavior

props are passed along to the component and to the DOM which will throw a warning.

Issue Analytics

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

github_iconTop GitHub Comments

9reactions
lcswillemscommented, Nov 15, 2019

I have just written an RFC for filtering forwarded props: https://github.com/styled-components/styled-components/issues/2878

Would you like a solution like this?

TL;DR It consists of adding a second parameter for filtering props. The current solution:

const StyledDiv = styled(({height, width, ...props}) => <div {...props} />)`
    width: ${props => props.width}px;
    height: ${props => props.height}px;
`

is turned into:

const StyledDiv = styled.div(`
    width: ${props => props.width}px;
    height: ${props => props.height}px;
`, ({width, height, ...props}) => props);

This is easy to write and to understand, and props filtering (which is not the important part of the definition) is sent at the end of the definition.

4reactions
probablyupcommented, Nov 2, 2020

We won’t be iterating further on this API. My recommendation would be to use transient props if you want something for styling purposes but not to be passed on. https://styled-components.com/docs/api#transient-props

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to extend styled component without passing props to ...
A prop that fails the test isn't passed down to underlying components, ... withConfig({ // Filter out the props to not be shown...
Read more >
API Reference - styled-components
This is a more dynamic, granular filtering mechanism than transient props. ... A prop that fails the test isn't passed down to underlying...
Read more >
React interactivity: Editing, filtering, conditional rendering
The Completed filter shows tasks whose completed prop is true . Beneath our previous addition, add the following — here we are using...
Read more >
Thinking in React
The component at the top of the hierarchy ( FilterableProductTable ) will take your data model as a prop. If you make a...
Read more >
React-admin - Filtering the List
To finish, create a <ListAction> component and pass it to the <List> component using the actions prop: import { TopToolbar, ExportButton } from...
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