情感CSS功能未呈现样式(反应)

时间:2018-11-28 18:30:39

标签: reactjs emotion css-in-js

我正在使用Webpack 4,Babel 7和Emotion 10。

我安装了@ emotion / core @ emotion / styled和推荐的插件babel-plugin-emotion

styled正常工作。我可以创建一个样式化的组件并使用它。但是,如果我尝试使用本机css``功能,它将呈现对象,而不呈现样式。

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

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

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

如果我检查标记,就会看到

有事!

如果我使用className而不是CSS,则相同。 另外,如果我是内联的或者使用css()功能。

如果我console.log()该对象的结果是:

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

任何人都知道发生了什么事...?我将不胜感激。我进行了研究,但找不到其他人提到此问题,因此可能做错了事。

3 个答案:

答案 0 :(得分:4)

您可能在div元素中以class="[object Object]"结尾。如果这就是您的全部代码, 您必须添加JSX编译指示,只需将/** @jsx jsx */放在jsx模块之前

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

...

并且在'bold'中没有单引号,这不是有效的CSS书写风格

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

答案 1 :(得分:1)

你应该像这样在模块的顶部添加 JSX pragma:

/** @jsx jsx */

pragma 可能不起作用,您应该使用

 /** @jsxImportSource @emotion/react */

相反。

答案 2 :(得分:0)

删除粗体属性中的单引号

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