Code Monkey home page Code Monkey logo

Comments (5)

mxstbr avatar mxstbr commented on May 18, 2024

I would expect this generates only the CSS with left: 0;. Is this correct?

Yes, and you can also dynamically change it and it will actually change accordingly! 😱

No idea how this works, but it does, after one second left will be set to 50:

const MyComponent extends React.Component {

  state = { left: 0 };

  render() {
    const FlyingBall = styled.div`
      position: absolute;
      width: 1rem;
      height: 1rem;
      left: ${this.state.left}
    `;

    setTimeout(() => {
      this.setState({
        left: 50
      });
    }, 1000);

    return <FlyingBall />;
}

from styled-components.

nikgraf avatar nikgraf commented on May 18, 2024

ok, this is cool. Now I'm curious if you used a development or production mode … or to rephrase: If I want to provide cacheable CSS file for production how does it work? Do you identify dynamic styles and set them via the style attribute or inject a style tag?

from styled-components.

mxstbr avatar mxstbr commented on May 18, 2024

inject a style tag?

This, I'm 99% sure. We use aphrodite under the hood, so there's no external CSS file at the moment. Not sure if we want to go down that route…

from styled-components.

geelen avatar geelen commented on May 18, 2024

Since #29 (and 0.2.0), we have simplified this. If you write:

let something;

const X = styled.div`
  ${ something }
`

Then the value of something will be locked to its initial value. There's no way around it, unless you call styled.div inside a render method like @mxstbr does above (you're effectively making a new component each time with whatever the current value of something is)

If you want something dynamic, though, just interpolate a function!

let something;

const X = styled.div`
  ${ () => something }
`

Now every time X is re-rendered that function will be reevaluated and the current value of something will be injected. Easy!

from styled-components.

nikgraf avatar nikgraf commented on May 18, 2024

Thanks @geelen for the clarification!

from styled-components.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.