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.

Emotion 10 - "React is not defined"

See original GitHub issue
  • emotion version: "@emotion/core": "^10.0.10"
  • react version: "react": "^16.8.6"
  • typescript version: "typescript": "^3.4.1"
  • react-scripts version: "react-scripts": "2.1.8"

Relevant code:

// AppContainer.tsx

/** @jsx jsx */
const AppContainer: React.SFC = () => (
  <ApolloProvider client={apolloClient}>
    <Global styles={() => css(normalize)} />

    <App />
  </ApolloProvider>
);
// App.tsx

const App: React.SFC = () => (
  <div className="App">
    <Header />
  </div>
);
// Header.tsx

/** @jsx jsx */
const Header: React.SFC = () => (
  <header css={headerCss}> ... </header>
);

What you did:

Started my application (react-scripts start)

What happened: “React is not defined” error.

image

Reproduction:

Many people are reporting this issue as responses to other existing issues, so I’m opening a thread here. I don’t have time to put together a code sandbox for it at the moment, but the error seems to occur when you have components which match the following criteria:

  • component uses the @emotion/core css function
  • component uses jsx pragma
  • component has children which meet the criteria:
    • child component doesn’t use jsx pragma
    • child component has children which use the jsx pragma

@bitten put together a code sandbox here: https://codesandbox.io/s/l7zzpkmmlm . If you change the text a couple times in index and/or ComponentTwo, it should reproduce consistently.

Problem description:

When using jsx pragma, sometimes the user gets a “React is not defined” error.

Other folks have done some debugging and found that webpack may actually treeshaking out React in these cases. This may indicate it’s an issue beyond emotion and that create-react-app may just not be compatible with this version. I cannot confirm.

Suggested solution:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:19
  • Comments:27 (1 by maintainers)

github_iconTop GitHub Comments

18reactions
justingrantcommented, Nov 18, 2019

FYI for other folks arriving here from Google, I also ran into this error when using <> which is the shorthand syntax for <React.Fragment>. The <> syntax is currently not compatible with emotion 10’s css prop. To work around the problem, use <React.Fragment> instead of <>.

For more details, see https://github.com/yannickcr/eslint-plugin-react/issues/2156 and https://github.com/emotion-js/emotion/issues/1549.

4reactions
JustinTRosscommented, Apr 8, 2019

@nathanshelly @bitttttten Are you both using CRA, Typescript, and Emotion?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Emotion.js with create-react-app: ReferenceError: jsx is not ...
I am trying to use emotion.js with ...
Read more >
Server Side Rendering - Emotion
Server side rendering works out of the box in Emotion 10 and above if you're only using @emotion/react and @emotion/styled . This means...
Read more >
The Mysterious Case of Emotion and “exports is not defined”
js presets after @babel/preset-react . I followed those steps, ran Webpack, and promptly got the error “ReferenceError: exports is not defined”.
Read more >
Emotion in React - LogRocket Blog
In this post, we will learn more about Emotion, and see the features and advantages that it can bring to our applications.
Read more >
Emotion - DEV Community ‍ ‍
The Mysterious Case of Emotion and "exports is not defined" ... How to setup create-react-app with twin.macro and emotion ... Follow. Mar 10...
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