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.

Disable automatic passing down of props

See original GitHub issue

Version

1.0.5

Steps to reproduce

    import { Link } from 'react-router'

    const ListElement = styled(Link)`

      ${props => props.active && activeListElement}
    `

Expected Behavior

Work without warnings

Actual Behavior

screen shot 2016-10-24 at 1 55 05 pm

Is it possible to disable the automatic pass down of props so we don’t have that warning anymore?

Thanks!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:11
  • Comments:30 (5 by maintainers)

github_iconTop GitHub Comments

212reactions
vdanchenkovcommented, Oct 25, 2016

Do you mean some props selectively? I’d suggest:

const ListElement = styled(({ active, ...rest }) => <Link {...rest} />)`
92reactions
alansouzaticommented, Jan 18, 2019

I’m facing this problem too. I don’t think const ListElement = styled(({ active, ...rest }) => <Link {...rest} />) is bullet proof solution. Here is where it is failing for me:

1) Callers can pass as prop and now the component suddenly breaks. for example:

<ListElement as="span" />

One would expect that it would render a Link as a span tag, but it gets rid of the Link altogether. Don’t get me wrong, this is the expected behavior in styled-components, but it makes it difficult customize things. I had to introduce a tagName prop just for that. And I keep getting the question why you need “tagName” since styled-components has “as” prop?

2) Hard, if not impossible to extend styles.

I have a Box component with some styles. This Box has a color prop that I need to exclude (otherwise it will bleed). Now I want a Flex component that augments this Box with flexbox capabilities. This Flex has a direction prop that I need to exclude. For example:

const Box = styled(({ color, ...rest}) => <div {...rest} />)`
  color: ${props => props.color};
`;

const Flex = styled(Box)`
  flex-direction: ${props => props.direction};
`;

Now if you inspect the DOM Box color does not bleed, but Flex direction does. I’m failing to identify how to properly extend these styles.

3) Having to deal with forwardRef and increasing the React DOM Tree.

Since I’m adding ({ active, ...rest }) => <Link {...rest} /> forward refs don’t work anymore. Meaning <ListElement ref={React.createRef()} />the ref will be the React element instead of the actual DOM, forcing me to use a deprecated findDOMNode API. Or wire the forwardRef which increases the React Depth Tree (which brings performance issues in React dev env)

I feel that everything would be much easier if styled-component allowed an API to exclude props to bleed to the DOM:

const Box = styled("div", { excludeProps: ['color'] })`
  color: ${props => props.color};
`;

const Flex = styled(Box, { excludeProps: ['direction'] })`
  flex-direction: ${props => props.direction};
`;
Read more comments on GitHub >

github_iconTop Results From Across the Web

React: Are props objects passed automatically to children ...
If you dont pass the props to the child components, they wont be available to the child component period. Have a look at...
Read more >
How passing props to component works in React
Master how to pass props (properties) to components in React with this useful beginner's guide, including demo code.
Read more >
React Props Cheatsheet: 10 Patterns You Should Know
If you have a lot of props that you're passing down to your component, it may be best to include them on the...
Read more >
The Disabled Attribute in React Buttons - Upmostly
Then, we define a function handleClick which uses the selectFruit function passed in props as a callback. We call it with the argument...
Read more >
Forwarding Refs - React
We pass our ref down to <FancyButton ref={ref}> by specifying it as a JSX attribute. React passes the ref to the (props, ref)...
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