24

This is my code

import { css, jsx } from '@emotion/core'
       
return (
       <>
       <img css={css`height: 200px; height: 200px;`} fluid={image.sharp.fluid} />
       <List>
           <li>Our Clients</li>
           <li>What We Do</li>
           <li>Contact</li>
       </List>
       </>
   )

This is the error I'm getting

You have tried to stringify object returned from css function. It isn't supposed to be used directly (e.g. as value of the className prop), but rather handed to emotion so it can handle it (e.g. as value of css prop).

This error seems to be telling me that I need to do what I am already doing? Or am I missing something here.

1
  • can you share your .babelrc ? Commented May 14, 2020 at 12:18

8 Answers 8

29

Just wanted to note I had this issue and adding this fixed the error for me:

/** @jsx jsx */
import { jsx } from '@emotion/core';

Just to clarify, you have to add /** @jsx jsx */; at the top of the file.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I think this should highlight in the emotion documentation too
This is highlighted in the emotion docs now under the Typescript section. Note that it precludes you from using <></> shorthand. You can still use Fragment emotion.sh/docs/typescript
@shallow.alchemy What if we're not using typescript ?
Thanks this saved me a lot of messing around i thought that was a comment! I found this updated version: /** @jsxImportSource @emotion/react */
29

This method solved my problem:

/** @jsxImportSource @emotion/react */
import { css } from "@emotion/react"

export default function somepage() {

  const color = 'red'

  return (<div
    css={css`
      background-color: hotpink;
      &:hover {
        color: ${color};
      }
    `}
  >
    Hello World.
  </div>)
}

What Does this do?

If you are using a zero-config tool with automatic detection of which runtime (classic vs. automatic) should be used and you are already using a React version that has the new JSX runtimes (hence runtime: 'automatic' being configured automatically for you) such as Create React App 4 then /** @jsx jsx */ pragma might not work and you should use /** @jsxImportSource @emotion/react */ instead.

3 Comments

What does it do ?
This worked for me! From that link above: If you are using a zero-config tool with automatic detection of which runtime (classic vs. automatic) should be used and you are already using a React version that has the new JSX runtimes (hence runtime: 'automatic' being configured automatically for you) such as Create React App 4 then /** @jsx jsx */ pragma might not work and you should use /** @jsxImportSource @emotion/react */ instead.
17

As detailed in the Emotion documentation, use of the css prop requires you to replace React with jsx as the target function for JSX elements (known as the “pragma”), which will enable Emotion to intercept the css prop and transform it into a className prop for React.

For example:

<p>Hi</p>

// Becomes the following by default:
React.createElement("p", {}, "Hi")

// But with the `jsx` pragma, it becomes:
jsx("p", {}, "Hi")

There are two outlined approaches to achieve this. You only need to select one. If you are able to configure Babel in your application, the first is the recommended approach, but either one works fine:

  1. Install a Babel plugin that will configure jsx as the default handler for all code in your app

    The most direct way to do this is by adding the relevant Babel preset to your Babel configuration like so:

    // Option 1A — Good
    // Add @emotion/babel-preset-css-prop to your dev dependencies, then
    // add the preset to your .babelrc configuration:
    
    {
      "presets": ["@emotion/babel-preset-css-prop"]
    }
    

    If you are able to do this, though, I recommend instead configuring babel-plugin-emotion, which includes the css prop configuration as well other features like minification, dead code elimination, and hoisting:

    // Option 1B — Better
    // Add babel-plugin-emotion to your dev dependencies, then add
    // the plugin to your Babel configuration (e.g. .babelrc)
    {
      "plugins": ["emotion"]
    }
    
  2. Import the jsx function from Emotion and instruct Babel to use this imported function on a per-file basis using pragma

    /** @jsx jsx */
    import { jsx } from '@emotion/core'
    

4 Comments

In my case after following your suggestion I was encountering **[this]( stackoverflow.com/questions/66965774/…) problem and replacing step 2 with/** @jsxImportSource @emotion/react */ fixed it.
I am on react 16.8.6 and cant change it. I am using emotion react 11.13. In my babel file I have presets: [ [ '@babel/preset-react', { runtime: 'automatic', importSource: '@emotion/react' } ], And use this plugin: '@emotion/babel-plugin' I run tests with enzyme and react 16 adapter + jasmine.
PART2: In one of my components that uses css={{}} I import this at the top: /** @jsxImportSource @emotion/react */ // eslint-disable-next-line no-unused-vars import { jsx } from '@emotion/react'; AND When running my tests I see: moduleName: './node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.esm.js', loc: '1:0-53', message: "Module not found: Error: Can't resolve 'react/jsx-runtime' in 'myRepo/node_modules/@emotion/react/jsx-runtime/dist'", moduleId: './node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.esm.js',
For example I use: css={{ whiteSpace: 'nowrap' }} But this is no longer working as is.
3

You need to install this babel plugin, babel-preset-css-prop

Comments

3

In addition to include the @emotion/babel-plugin, there was some configuration that I have missed that created that error.

{
  "presets": [
    [
      "@babel/preset-react",
      { "runtime": "automatic", "importSource": "@emotion/react" }
    ]
  ],
  "plugins": ["@emotion/babel-plugin"]
}

The "importSource": "@emotion/react" was missing. After adding it, the error was gone and the styles were implemented correc5y.

Comments

2

There are 2 ways to get started with the css prop.

  1. Babel Preset
  2. JSX Pragma

Both methods result in the same compiled code.


JSX Pragma

Set the jsx pragma at the top of your source file that uses the css prop. This option works best for testing out the css prop feature or in projects where the babel configuration is not configurable (create-react-app, codesandbox, etc.).

/** @jsx jsx */

if it didn't work use:

/** @jsxImportSource @emotion/react */

Babel Preset

This method will not work with Create React App or other projects that do not allow custom Babel configurations. Use the JSX Pragma method instead.

.babelrc

{
  "presets": ["@emotion/babel-preset-css-prop"]
}

Couldn't solve your problem? Consider reading official documentation: https://emotion.sh/docs/css-prop

Comments

1

If you're using Vite, dont forget to add following setting in your vite.config.ts :

export default defineConfig({
  plugins: [
    react({
      jsxImportSource: '@emotion/react',
    }),
  ]
})

It allowed me to fix that issue.

Comments

-6

/**@jsx jsx */ import { jsx, css } from '@emotion/core'

1 Comment

Welcome to Stack Overflow. Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain what the code does and how it answers the question, so that it is also useful for other users with similar a problem, as well as the OP. Also, please format your code correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.