styled-jsx: Error "insertRule` accepts only strings" when using with styled-components

What is the current behavior?

  1. Get with-styled-components example (https://github.com/zeit/next.js/tree/canary/examples/with-styled-components)
  2. Add <styled jsx tag to the index.js file so you got something like:
export default () => (
    <div>
        <h1>hello</h1>
        <style jsx={true} global={true}>
            {`
                h1 {
                    color: red;
                }
            `}
        </style>
    </div>
)

  1. Error appears:
StyleSheet: `insertRule` accepts only strings.
  1. When i remove the "styled-components" plugin from .babelrc, everything works fine.

What is the expected behavior?

Styled-jsx tags should work with styled-components + babel-plugin-styled-components in stack.

Environment (include versions)

  • OS: OSX 10.13.4
  • Browser: Chrome 70.0.3538.102
  • styled-jsx (version): 3.1.0

Did this work in previous versions?

IIRC this was working in my older project with Next.js 6.1.1.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (3 by maintainers)

Commits related to this issue

Most upvoted comments

I think I have found the cause. babel-plugin-styled-components > 1.8.0 can properly enable the css prop of styled-components objects, whereas the same prop will be ignored in babel-plugin-styled-components <= 1.8.0. The css prop then conflicts with styled-jsx and cause the program to fail.

@lucifer1004’s solution worked for me as well. Thanks 😃

we can switch from

<Style styleId={123} css={''} />

to

<Style id={123} children={''} />