'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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIkM6XFxwcmFjdGljZVxcbm9jcmFcXHNyY1xcQXBwLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQXFCZ0IiLCJmaWxlIjoiQzpcXHByYWN0aWNlXFxub2NyYVxcc3JjXFxBcHAuanMiLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgUmVhY3QsIHsgQ29tcG9uZW50IH0gZnJvbSAncmVhY3QnO1xyXG5pbXBvcnQgeyBjb25uZWN0IH0gZnJvbSAncmVhY3QtcmVkdXgnO1xyXG5cclxuaW1wb3J0IHsgZmV0Y2hDb21tZW50cyB9IGZyb20gJy4vYWN0aW9ucyc7XHJcbmltcG9ydCB7IGNzcywganN4IH0gZnJvbSAnQGVtb3Rpb24vY29yZSc7XHJcbmltcG9ydCBzdHlsZWQgZnJvbSAnQGVtb3Rpb24vc3R5bGVkJztcclxuXHJcbmltcG9ydCBzdHlsZXMgZnJvbSAnLi9BcHAuY3NzJztcclxuaW1wb3J0IGNzc0luSnMgZnJvbSAnLi9BcHAuY3NzLmpzJztcclxuXHJcbmltcG9ydCByUEkgZnJvbSAnLi9pbWFnZXMvclBJLTQwMHg0MDAuanBnJztcclxuXHJcbmNvbnN0IENvbW1lbnRzQm94ID0gc3R5bGVkKCdkaXYnKWBcclxuICBiYWNrZ3Jv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XG4gIHJldHVybiB7XHJcbiAgICBjb21tZW50c1xyXG4gIH07XHJcbn07XHJcblxyXG5leHBvcnQgZGVmYXVsdCBjb25uZWN0KG1hcFN0YXRlVG9Qcm9wcywgeyBmZXRjaENvbW1lbnRzIH0pKEFwcCk7XHJcbiJdfQ== */"
}
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.
Solution 1:[1]
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
Solution 2:[2]
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.
Solution 3:[3]
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.
Solution 4:[4]
Remove the single quote in the bold attribute
const bold = css`
font-weight: bold;
`;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | mohammad barzegar |
Solution 3 | Moffen |
Solution 4 | mesvil |