Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime error with css prop and const declared after component #260

Closed
itsMapleLeaf opened this issue Nov 11, 2019 · 0 comments · Fixed by #334
Closed

Runtime error with css prop and const declared after component #260

itsMapleLeaf opened this issue Nov 11, 2019 · 0 comments · Fixed by #334

Comments

@itsMapleLeaf
Copy link

itsMapleLeaf commented Nov 11, 2019

Example:

// in:
import React from "react"
import { css } from "styled-components"

export default function Example() {
  return <div css={someCss}>oops</div>
}

const someCss = css`
  color: red;
`

// out:
import _styled from "styled-components";
import React from "react";
import { css } from "styled-components";

var _StyledDiv = _styled.div.withConfig({
  displayName: "Example___StyledDiv",
  componentId: "sc-1j0h0n6-0"
})(["", ""], someCss);

export default function Example() {
  return <_StyledDiv>oops</_StyledDiv>;
}
const someCss = css(["color:red;"]);

At runtime, this code will fail, as it tries to access someCss before it's declared.

This works if we declare someCss before Example. The someCss style is correctly placed before the styled component.

// in:
import React from "react"
import { css } from "styled-components"

const someCss = css`
  color: red;
`

export default function Example() {
  return <div css={someCss}>oops</div>
}

// out:
import _styled from "styled-components";
import React from "react";
import { css } from "styled-components";
const someCss = css(["color:red;"]);

var _StyledDiv = _styled.div.withConfig({
  displayName: "Example___StyledDiv",
  componentId: "sc-1j0h0n6-0"
})(["", ""], someCss);

export default function Example() {
  return <_StyledDiv>oops</_StyledDiv>;
}

Repro: https://github.com/kingdaro/styled-components-css-prop-error

Downgraded to 1.10.2 as a workaround for now. It looks like this commit introduced the change in behavior: 9bc3c08

sfishel18 added a commit to sfishel18/babel-plugin-styled-components that referenced this issue Aug 17, 2020
in case it references a variable that is declared after the react component that uses it

fixes styled-components#260
quantizor pushed a commit that referenced this issue Jun 29, 2021
in case it references a variable that is declared after the react component that uses it

fixes #260
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant