LoginSignup
3
0

More than 1 year has passed since last update.

@emotion/core は @jsx jsx を書かなくても使える、の続き

Last updated at Posted at 2021-05-20

問題

emotion v11では上のようにはうまく動かない様子。

解決

  1. /** @jsx jsx */の除去
  2. Property 'css' does not exist on type ...エラーの除去

/** @jsx jsx */

core->react
$ yarn remove @emotion/core
$ yarn add @emotion/react
$ yarn add -D @emotion/babel-preset-css-prop
.babelrc
{
  "presets": ["@emotion/babel-preset-css-prop"]
}

オプション等あるが、基本これだけ。

Property 'css' does not exist on type ...

.d.ts相当のファイル
/// <reference types="@emotion/react/types/css-prop" />

自分はnextjsを使っているので、next-env.d.tsに追加した。

サンプル

import { css } from "@emotion/react"
import React from "react"
interface Props {}

const test: React.FC<Props> = (props) => {
  return (
    <div
      css={css`
        p {
          background-color: blueviolet;
        }
        span {
          background-color: burlywood;
        }
      `}
    >
      <p>aaaaa</p>
      <span>bbbbb</span>
    </div>
  )
}
export default test

個人的には
import { css } from "@emotion/react"
をしたほうが圧倒的に便利。cssにインテリセンスが効く。

Screen Shot 2021-05-21 at 8.15.25.png

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0