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.

Styled component not transforming on prop change

See original GitHub issue

Hi,

Having a problem with a side drawer component that is supposed to slide in from the right on open. Wrote it in a separate CSS file first and it works like a charm, but for some reason styled-components ignores the fact that the props.open changes. I tried logging the props and the component receives it as it should.

Here is my code:

const SideDrawerBox = styled.div position: fixed; width: 280px; max-width: 70%; height: 100%; right: 0; top: 0; z-index: 200; background-color: #2E2E3A; padding: 32px 16px; box-sizing: border-box; transition: transform 0.3s ease-out;

transform: translateX(${props => props.open ? '0%' : '+100%' });

@media(min-width:1024px){
  display: none;
}

``

<SideDrawerBox> <SideDrawerItems> <SideDrawerLogo src={Logo} /> <SideDrawerText>Register</SideDrawerText> <SideDrawerText>Log in</SideDrawerText> </SideDrawerItems> </SideDrawerBox>

Any ideas? Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

38reactions
kittencommented, Nov 29, 2017
const sideDrawer = (props) => {

  // *** STYLE *** //

  const SideDrawerBox = styled.div`

You’re recreating the styled component SideDrawerBox over and over again on every render. This will cause React to recycle that part of your element tree and readd everything again.

Very expensive for all moving parts that are involved 😉

You’ll always want to have every component, including especially styled components, outside of other component’s render cycle, or any other method 😄

15reactions
kittencommented, May 31, 2019

@bailycase as stated above, the StyledComponent is recreated every time the parent component renders. Like any component this will cause React not to recycle the underlying DOM structure, which will mean that no animation actually plays since the DOM node is destroyed.

We create actual components so they’ll have to be kept static across the life cycle of a component.

If you’re still experiencing issues and need help though it might make sense to open a new issue and ask for help with your specific snippet of code 🙌

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - Transform function not working in styled components
I am trying to change the styled of "IconRadioButtonRound" component based on the "HiddenCheckBox" value.I am able to change the bakcground ...
Read more >
FAQs - styled-components
If you were using the innerRef prop, change it to a normal ref.. const Component = styled.div`.
Read more >
Use with React - Styletron
Motivation; Styled Components; Props Filtering; $as prop; $style prop ... a slight modification to an existing styled component and reuse is not a...
Read more >
Demystifying styled-components - Josh W Comeau
In the end, we're left with a plain ol' string with populated values. When the props change, the process is repeated, and a...
Read more >
Styled Components in React — All you need to know
The “AS” Polymorhic Prop ... Sometime you might need to change the final tag or component that is rendered. You might for instance...
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