styled-jsx: I got _JSXStyle is not defined with 3.4.0

👋 friend. Welcome to styled-jsx and thanks for contributing!

⚠️ IMPORTANT ⚠️

If you need help or have a question about styled-jsx please ask it on Spectrum https://spectrum.chat/styled-jsx or join our Slack community at https://zeit.chat and ask it in the #next channel.

Before you open a new issue please take a look at our Frequent Asked Questions and open issues.

Remember, often time asking in chat or looking at the FAQ/issues can be faster than waiting for us to reply to a new issue*.

If you are here to report a bug or request a feature please remove this introductory section and fill out the information below!


Do you want to request a feature or report a bug?

What is the current behavior?

when I use <style jsx> in MyComponent I got _JSXStyle is not defined

If the current behavior is a bug, please provide the steps to reproduce and possibly a minimal demo or testcase in the form of a Next.js app, CodeSandbox URL or similar

  1. webpack 4.42.0
  2. mac

What is the expected behavior?

Environment (include versions)

  • OS:
  • Browser:
  • styled-jsx (version): 3.4.0

Did this work in previous versions?

yes

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 19
  • Comments: 20 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Hey guys, I ran into a similar issue and just found a workaround for my use case. Hopefully it can shed a light on yours?

TL;DR

import _JSXStyle from 'styled-jsx/style';
 
if (typeof global !== 'undefined') {
  Object.assign(global, { _JSXStyle })
}
More context

I was actually trying to import a component from a local package (yarn workspaces + lerna setup) that had <style jsx> blocks and styled-jsx@3.4.4 as external dependency.

Next app (website) package.json

{
  "name": "@clients/test",
  "version": "1.0.0",
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "@lib/react-ui": "^1.0.0",
    "next": "^10.0.9",
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  }
}
import React, { Fragment } from 'react'
import Head from 'next/Head'

const App = ({ Component, pageProps }) => {

  return (
    <Fragment>
        <Component {...pageProps} />
        {/* added the lines below just for the sake of it */}
        <style global jsx>{`
          body::-webkit-scrollbar {
            width: 0;
          }
        `}</style>
    </Fragment>
  )
}

export default App

Next app (component lib) package.json

{
  "name": "@lib/react-ui",
  "version": "1.0.0",
  "main": "dist/index.js",
  "module": "esm/index.js",
  "files": [
    "/dist",
    "/esm"
  ],
  "devDependencies": {
    "@babel/cli": "^7.13.0",
    "@babel/core": "^7.13.8",
  },
  "dependencies": {
    "styled-jsx": "^3.4.4"
  }
}
import React, { Fragment } from 'react'

const ExampleComponent = () => {

  return (
    <Fragment>
      <span className="avatar" {...props}>
          {text}
        </span>
        <style jsx>{`
          .avatar {
            display: inline-block;
            position: relative;
        `}</style>
    </Fragment>
  )
}

export default ExampleComponent

Adding the snippet below inside the component library app index.js file did the trick. The _JSXStyle is now being correctly imported/compiled by webpack.

import _JSXStyle from 'styled-jsx/style';
 
if (typeof global !== 'undefined') {
  Object.assign(global, { _JSXStyle })
}

Adding the above directly inside the Next app (website) and installing styled-jsx@3.4.4 as a dependency did also fix the problem whilst troubleshooting.

Inspired on this article

I got error _JSXStyle is not defined when my components use destructuring; import _JSXStyle2 from "styled-jsx/style"; but it should be import _JSXStyle from "styled-jsx/style";

@Timer I noticed that versions prior to 3.3.3 are not compatible with React 17+, although the current issue can be avoided. Can we temporarily release a tag version of dev / alpha / beta to roll back the issue so that some users can use styled-jsx properly first?

This does not affect current users, it is just for those who want to avoid this issue, perhaps you have other solutions and would like to hear your ideas.