Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emotion css feature is not rendering the styles (React)

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

Something!

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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIkM6XFxwcmFjdGljZVxcbm9jcmFcXHNyY1xcQXBwLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQXFCZ0IiLCJmaWxlIjoiQzpcXHByYWN0aWNlXFxub2NyYVxcc3JjXFxBcHAuanMiLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgUmVhY3QsIHsgQ29tcG9uZW50IH0gZnJvbSAncmVhY3QnO1xyXG5pbXBvcnQgeyBjb25uZWN0IH0gZnJvbSAncmVhY3QtcmVkdXgnO1xyXG5cclxuaW1wb3J0IHsgZmV0Y2hDb21tZW50cyB9IGZyb20gJy4vYWN0aW9ucyc7XHJcbmltcG9ydCB7IGNzcywganN4IH0gZnJvbSAnQGVtb3Rpb24vY29yZSc7XHJcbmltcG9ydCBzdHlsZWQgZnJvbSAnQGVtb3Rpb24vc3R5bGVkJztcclxuXHJcbmltcG9ydCBzdHlsZXMgZnJvbSAnLi9BcHAuY3NzJztcclxuaW1wb3J0IGNzc0luSnMgZnJvbSAnLi9BcHAuY3NzLmpzJztcclxuXHJcbmltcG9ydCByUEkgZnJvbSAnLi9pbWFnZXMvclBJLTQwMHg0MDAuanBnJztcclxuXHJcbmNvbnN0IENvbW1lbnRzQm94ID0g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Byb3BzID0gKHsgY29tbWVudHMgfSkgPT4ge1xyXG4gIHJldHVybiB7XHJcbiAgICBjb21tZW50c1xyXG4gIH07XHJcbn07XHJcblxyXG5leHBvcnQgZGVmYXVsdCBjb25uZWN0KG1hcFN0YXRlVG9Qcm9wcywgeyBmZXRjaENvbW1lbnRzIH0pKEFwcCk7XHJcbiJdfQ== */"
}

Anyone knows what´s going wrong...? I will appreciate any help. I researched but could not find other person referring to this issue so probably m doing something wrong.

like image 754
Peter Avatar asked Nov 28 '18 18:11

Peter


People also ask

Does emotion use styled-components?

styled is a way to create React components that have styles attached to them. It's available from @emotion/styled. styled was heavily inspired by styled-components and glamorous.

Is emotion better than styled-components?

The biggest advantage of Emotion is its easily handled object styles for writing CSS. Take, for example, the case of styled-components, wherein the developer must create unique names for different components, all while avoiding identical naming styles.

Is emotion CSS-in-JS?

Emotion is a library designed for writing css styles with JavaScript.

How do I install emotions in React?

There are lots of ways to use Emotion, if you're using React, the easiest way to get started is to use the @emotion/react package. If you're not using React, you should use the @emotion/css package. To use it, import what you need, for example use the css prop to create class names with styles.


2 Answers

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

like image 183
童Michael Avatar answered Oct 09 '22 21:10

童Michael


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.

like image 6
mohammad barzegar Avatar answered Oct 09 '22 22:10

mohammad barzegar