Guide on utilizing emotion's CSS with TypeScript?

I'm currently using emotion within a create-react-app setup with Typescript, and I want to utilize the css prop from emotion.

Following the guidelines in their best practices, my aim is to define my styles outside of the component using a const:

import { css } from "@emotion/react";

const customStyle = css({
  backgroundColor: "red",
  display: "inline-block",
  height: 24,
  width: 24
});

export default function App() {
  return <div css={customStyle}></div>;
}

I've followed their specific steps for implementing the css prop with Typescript and adjusted my TypeScript compiler options as follows:

"jsx": "react-jsx",
"jsxImportSource": "@emotion/react"

However, despite this configuration, the styles are not being applied. Instead, a div displaying an error 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>

I'm puzzled why the styles aren't taking effect. Additionally, I have created a simple demo on codesandbox.

Answer №1

If you want to include styling using @emotion/css package instead of @emotion/react, you have the option to assign the className manually.

Check out the code snippet below:

import { css } from "@emotion/css";

const customStyle = css({
  backgroundColor: "blue",
  display: "flex",
  height: 32,
  width: 32
});

export default function Component() {
  return <div className={customStyle}>Your content here</div>;
}

To learn more about this approach, refer to the documentation at

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Javascript alert function for confirming background color

Options are displayed in blue, and using JavaScript, I check if the background color is blue. If it's not, execute the next step; otherwise, display an alert message. function click_option_A() { var clr_1_chk = document.getElementById("1"); //alert ...

``Error Message: TypeORM - could not establish database connection

I encountered an issue while running my project built with Typescript, Typeorm, and Express. The error message received when running the dev script was: connectionNotFoundError: Connection "default" was not found The content of my ormconfig.json ...

Creating a .d.ts file for a JavaScript file that exports a plain object - tips and best practices

I am attempting to include a .d.ts file for an existing js file. The type.js file looks like this: // type.js export default { NORMAL: '001', CHECK: '002', }; Now, I have added a type.d.ts file as follows: // type.d.ts decla ...

Delay the axios get request in the useEffect

Working with React JS, I have implemented a useEffect hook to request data from a database via an Express server when the page renders. However, if the server is down, the app will continue to make thousands of requests until it crashes. Is there a way to ...

Why do the props being retrieved from Redux seem to be undefined?

I downloaded the starter template from mern.io and encountered an issue when trying to run the code below. The error message I received was: "ReferenceError: props is not defined" EventDetailPage.js : import React, {PropTypes} from 'react'; imp ...

PHP script, creating a public function that manipulates an array while also incorporating CSS styles

Within a public function, I have the following PHP: [ 'type' => 'text', 'desc' => $this->l('some information'), ] Using this code will show the text "some information" on a webpage. Is there a way ...

The CSS attribute selector is unable to target dynamically appended class names

Utilizing React's CSS animations add-on has been a seamless process, with everything running smoothly when using the provided classnames. .quote-enter { opacity: 0.01; transition: opacity .25s ease-in; } .quote-enter.quote-enter-active { opaci ...

Aligning text in the center within a PHP file

Presently, I have a script set up to act as a menu page where the user's details are verified before they can choose from three different tours. To improve the aesthetics of the page, I want to centralize the text. However, when I attempt to do so, th ...

What is the most efficient way to send various props to multiple child components within a React parent component?

Currently, I am working on developing a color palette generator using React. However, I have encountered an issue where all child divs in the color palette receive the same color instead of different colors. Below is the code snippet: class ColorPalet ...

What is the proper way to utilize the > symbol in React JSX?

In my code, I have a list of breadcrumbs that I am mapping over and generating links for. However, I want to add an optional '>' if the index is less than the total number of crumbs in the array - but I keep encountering parsing errors no matt ...

What could be the reason for ngOnChanges lifecycle hook not getting invoked?

I am currently experimenting with Angular 2 in an effort to learn more about it. I noticed that ngOnChanges is not triggering in the code below: app.component.ts: import { Component, Input } from "@angular/core" import { FormsModule } from '@angular ...

I am struggling to create this design using Material UI in my REACT project

I'm having trouble coding the highlighted middle layout in pink circle The desired layout I am aiming to achieve this specific design using MaterialUI in React ...

Dynamic and deriveStateFromProps - Animate this!

Given that componentWillReceiveProps is being phased out in favor of getDerivedStateFromProps, which returns a final state, how can I achieve gradual state changes (e.g. animations) using the new getDerivedStateFromProps method? The following code snippet ...

Narrow down your search results with date range filtering in Angular Material

Seeking guidance as a newcomer to Angular Material, I am trying to implement a date range filter for a table of N results. The options in the filter select are (1 day, 5 days, 1 week, 15 days), which are populated using a variable JS vm.rangos=[ {id ...

Vertically align header 3 text with a specific height

Recently, I encountered an issue with a h3 <h3>someText<h3> This particular h3 has a height and features a background image h3 { height:30px; background-image:url(); background-reat: repeat-x; } I am currently facing a challenge where the t ...

What steps should I take to ensure proper spacing on my website? (admin inquiry)

I'm currently working on a website called www.confiam.com.br, which is a project built with Django. While making some adjustments recently, I unintentionally broke something related to the formatting of paragraphs. Now, the spacing doesn't seem r ...

Issue: formGroup requires an input of type FormGroup. Please provide one; Error: Unable to access property 'controls' as it is undefined

In the process of building a login and registration form using Angular with .Net core, I encountered an error upon running the program. The error is showing up in the Browser Console tab. This is my userlog.component.ts file: import { Component, OnInit, O ...

The Datalabels in Sunburst Highchart are being concealed outside of the svg container

Within the Sunburst highchart, I have positioned the data labels outside the circle using x and y coordinates. dataLabels: { y: -75, rotation: 0, x: -15 } However, if I assign a specific value to the x or y axis (e.g. y: -110) and the labels extend ...

Tips for adjusting the background color of an HTML section based on array outcomes

I created an array that randomly selects a movie, and below I have movie posters. When I click on a poster, it takes me to the movie on Netflix. It would be really helpful if the background color of the selected movie poster changed to green for easier ide ...

The fixed div transforms its content when other divs move through it during scrolling

I am looking to create a fixed div at the top of a layout that will display information about a blog post, such as the date posted, number of notes, and permalink. As users scroll past different posts, I want this fixed div to update with the relevant in ...