I use emotion in a create-react-app with Typescript and I would like to use the css prop of emotion.
Following their best practices I would like to define my styles in a const outside of my components, like so:
import { css } from "@emotion/react";
const myStyle = css({
backgroundColor: "red",
display: "inline-block",
height: 24,
width: 24
});
export default function App() {
return <div css={myStyle}></div>;
}
I followed their instructions for setting up the css prop with Typescript and configured my Typescript compiler options like so:
"jsx": "react-jsx",
"jsxImportSource": "@emotion/react"
however the style is not applied and instead a div containing the following message is rendered:
<div css="You have tried to stringify object returned from `css` function. It isn't supposed to be used directly (e.g. as value of the `className` prop), but rather handed to emotion so it can handle it (e.g. as value of `css` prop)."></div>
Why are the styles not applied?
I also made a basic sample on codesandbox.
/** @jsxImportSource @emotion/react */inside App.tsxcreate-react-app: github.com/facebook/create-react-app/issues/9847. But the documentation ofemotionis confusing, too: github.com/emotion-js/emotion/issues/2662