11

I'm using Webpack 4, Babel 7 and Emotion 10.

I installed @emotion/core, @emotion/styled, and the recommended plugin babel-plugin-emotion.

styled is working properly. I can create a styled component and use it. However, if I try to use the native css`` feature it renders the object but not the styles.

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

const bold = css`
  font-weight: 'bold';
`;

render(
...
<div css={bold}>Something!</div>
...
)

If I inspect the markup, I see:

<div css="[object Object]">Something!</div>

If I use className instead of css, is the same. Also if I do it inline or if I use the css() feature.

And If I console.log() that Object the result is:

{
  "name": "15b6lyx-bold",
  "styles": "font-weight:'bold';label:bold;",
  "map": "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIkM6XFxwcmFjdGljZVxcbm9jcmFcXHNyY1xcQXBwLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQXFCZ0IiLCJmaWxlIjoiQzpcXHByYWN0aWNlXFxub2NyYVxcc3JjXFxBcHAuanMiLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgUmVhY3QsIHsgQ29tcG9uZW50IH0gZnJvbSAncmVhY3QnO1xyXG5pbXBvcnQgeyBjb25uZWN0IH0gZnJvbSAncmVhY3QtcmVkdXgnO1xyXG5cclxuaW1wb3J0IHsgZmV0Y2hDb21tZW50cyB9IGZyb20gJy4vYWN0aW9ucyc7XHJcbmltcG9ydCB7IGNzcywganN4IH0gZnJvbSAnQGVtb3Rpb24vY29yZSc7XHJcbmltcG9ydCBzdHlsZWQgZnJvbSAnQGVtb3Rpb24vc3R5bGVkJztcclxuXHJcbmltcG9ydCBzdHlsZXMgZnJvbSAnLi9BcHAuY3NzJztcclxuaW1wb3J0IGNzc0luSnMgZnJvbSAnLi9BcHAuY3NzLmpzJztcclxuXHJcbmltcG9ydCByUEkgZnJvbSAnLi9pbWFnZXMvclBJLTQwMHg0MDAuanBnJztcclxuXHJcbmNvbnN0IENvbW1lbnRzQm94ID0gc3R5bGVkKCdkaXYnKWBcclxuICBiYWN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4ge1xyXG4gIHJldHVybiB7XHJcbiAgICBjb21tZW50c1xyXG4gIH07XHJcbn07XHJcblxyXG5leHBvcnQgZGVmYXVsdCBjb25uZWN0KG1hcFN0YXRlVG9Qcm9wcywgeyBmZXRjaENvbW1lbnRzIH0pKEFwcCk7XHJcbiJdfQ== */"
}

Does anybody know what's going wrong?

5 Answers 5

13

You might end up with class="[object Object]" in div element. If that is all your codes, you should add JSX pragma /** @jsx jsx */ thing on top before jsx module

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

...

and no single quote at 'bold', it is not valid css writing style

https://emotion.sh/docs/css-prop

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

Comments

6

you should add JSX pragma the top of the module like this:

/** @jsx jsx */

pragma might not work and you should use

 /** @jsxImportSource @emotion/react */

instead.

1 Comment

your worked but I try this doesn't work: "presets": [ [ "@babel/preset-react", { "runtime": "automatic", "importSource": "@emotion/react" } ] ]
2

You need to make sure you have the JSX pragma compiler directive specified at the top of your file:

/** @jsxRuntime classic */
/** @jsx jsx */
import { jsx } from '@emotion/react'

See this tutorial for more info.

Comments

0

with create-react-app using @emotion/react 11, React 18, and Typescript 4.4, I was initially just getting css="[object Object]" in the DOM. Using the jsxImportSource pragma worked for me, but I did not want to add it to every file, so adding this to tsconfig.json instead worked for me:

// tsconfig.json
"compilerOptions": {
  ...,
  "jsxImportSource": "@emotion/react"
}

Just a few days layer I also ran into this exact same issue using rollup + esbuild (rollup-plugin-esbuild), and this solved it for me

esbuild({
  ...
  jsx: 'automatic',
  jsxImportSource: '@emotion/react',
}),

Comments

0

Different front-end tooling provides various ways to transform JSX. Sometimes you need to provide an explicit way for the tool to handle the JSX. My problem was when using Vite. You can add a custom plugin in your plugins section in your vite.config.ts handle the jsxImportSource problem:

Add this as the first plugin in your plugins array:

    {
      name: 'add-emotion-jsx-pragma',
      enforce: 'pre',
      transform(code: string, id: string): TransformResult | null {
        if (/\.[jt]sx?$/.test(id)) {
          return {
            code: `/** @jsxImportSource @emotion/react */\n${code}`,
            map: null,
          };
        }
        return null;
      },
    },

Note, like others mentioned - you still need to add the following to your tsconfig

// tsconfig.json
"compilerOptions": {
  ...,
  "jsxImportSource": "@emotion/react"
}

Comments

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.