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.

`as` removes bases styles for functional/class components

See original GitHub issue

Environment

System:

  • OS: macOS 10.14.2
  • Shell: 5.3 - /bin/zsh

Binaries:

  • Node: 8.9.0
  • Yarn: 1.12.3

npmPackages:

  • babel-plugin-styled-components: 1.5.1 => 1.5.1
  • styled-components: 4.1.2 => 4.1.2

Reproduction

https://codesandbox.io/s/30k0n99jv5

Steps to reproduce

  1. Create Styled-Component
  2. Use the Styled-Component in a functional component (be sure to pass down your props to the styled-component)
  3. Extend functional component with some additional styles
  4. Use your extended version of the component with the as as prop

Expected Behavior

  • Base styles should still be applied
  • Extended styles should be applied overtop of base styles
  • New tag is used

Actual Behavior

  • Base styles are dropped, and only the extended styles are applied
  • New tag is still properly applied.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
AustPcommented, Feb 10, 2019

I’m seeing this on 4.1.3 as well.

Headline.js:

import styled from 'styled-components';

const Headline = styled.h1`
  color: orange;
  display: block;
  font-weight: 300;
`;

export default Headline;

StyledHeadline.js:

import { Headline } from 'components';
import styled from 'styled-components';

const StyledHeadline = styled(Headline)`
  color: pink;
`;

When I use StyledHeadline with the as prop, the only styles applied are color: pink;.

2reactions
usagi-fcommented, Apr 2, 2019

I encountered this issue too.

In case of use styled-component for the UI-library that has internally gives className, and using as option will behave exactly as the pasted by guys the examples. This could not keep the appearance as enough UI component.

I read styled-component’s source code a little. For now, I just found behavior is below.

const elementToBeCreated = this.props.as || this.attrs.as || target;

https://github.com/styled-components/styled-components/blob/master/packages/styled-components/src/models/StyledComponent.js#L135

return createElement(elementToBeCreated, propsForElement);

https://github.com/styled-components/styled-components/blob/master/packages/styled-components/src/models/StyledComponent.js#L172

Is it generated another element when use as? Which means, Even if can be passed enough props object by propsForElement, the internal logic by the component itself that is originally passed as a target is not executed.

If the classNames within a component are internally generated by different architectures such as classnames, the component will be destroyed when use with as.

Is as use case only use to the primitive element?

This is might become a stall point when use styled-component with some UI library. Also, this may be breaking if the UI library has props of the same name (as).

If my understanding is not wrong, the best workaround is … “Take care when use as option” 🤕 Can I know the contributor’s opinion about this?

cc: @probablyup @leerob thanks for your example, It is easy to understand.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Functional vs Class Components in React Native
In React Native, there are two main types of components that make up an application: functional components and class components.
Read more >
React Class Component vs Functional Component - Telerik
If you're a React developer or new to React, this article clears up some of the confusion between these two types of components...
Read more >
How To Style React Components | DigitalOcean
In this tutorial, you'll learn three different ways to style React components: plain Cascading Style Sheets (CSS), inline styles with ...
Read more >
Understanding Functional Components vs. Class ... - Twilio
This article will help you understand the differences between functional and class components by walking through each one with sample code ...
Read more >
How To Use Styled-Components In React - Smashing Magazine
Adapting Based on Props #. Styled components are functional, so we can easily style elements dynamically. Let's assume we have two types of ......
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