Challenges encountered with Emotion (Styled Components) integration in Gatsby

I rely on Emotion for creating styled components in my projects, but I have encountered some issues. This is how I initially install it:

npm i @emotion/react @emotion/styled gatsby-plugin-emotion

For instance, when integrating it into the Header component, I follow these steps:

import React from 'react';
import { Link } from 'gatsby';
import Navegacion from './navegacion';
import { jsx, css } from '@emotion/react';

const Header = () => {
  return (
    <header
      css={css`
        background-color: #0d283b;
        padding: 1rem;
      `}
    >
      <div
        css={css`
          max-width: 120rem;
          margin: 0 auto;
          text-align: center;
          @media (min-width: 768px) {
            display: flex;
            align-items: center;
            justify-content: space-between;
          }
        `}
      >
        <Link>Bienes Raíces</Link>
       <Navegacion />
      </div>
    </header>
  )
}
 
export default Header;

However, upon running gatsby develop, an error occurs:

`WebpackError: The `@emotion/core` package has been renamed to `@emotion/react`. Please import it like this `import { jsx } from '@emotion/react'.

Can anyone offer assistance?

This is the content of my package.json file:

"dependencies": {
    "@emotion/core": "^11.0.0",
    "@emotion/react": "^11.1.1",
    "@emotion/styled": "^11.0.0",
    "gatsby": "^2.26.1",
    "gatsby-image": "^2.5.0",
    "gatsby-plugin-emotion": "^4.5.0",
    "gatsby-plugin-manifest": "^2.6.1",
    "gatsby-plugin-offline": "^3.4.0",
    "gatsby-plugin-react-helmet": "^3.4.0",
    "gatsby-plugin-sharp": "^2.8.0",
    "gatsby-source-filesystem": "^2.5.0",
    "gatsby-transformer-sharp": "^2.6.0",
    "prop-types": "^15.7.2",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-helmet": "^6.1.0"
  },

In addition, here is my gatsby-config file:

module.exports = {
  siteMetadata: {
    title: `Gatsby Default Starter`,
    description: `Kick off your next, great Gatsby project with this default starter. This barebones starter ships with the main Gatsby configuration files you might need.`,
    author: `@gatsbyjs`,
  },
  plugins: [
    `gatsby-plugin-emotion`,
    `gatsby-plugin-react-helmet`,
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/src/images`,
      },
    },
    `gatsby-transformer-sharp`,
    `gatsby-plugin-sharp`,  
  ],
}

Answer №1

The reason for this issue is due to changes in package names in Emotion 11. It seems that gatsby-plugin-emotion may not have been updated yet, which could be causing the error from the plugin code.

If you want to continue using your existing code, refer to the v10 Emotion documentation and ensure that the dependency version for @emotion/core is set to "~10.0.0" in your package.lock file.

In Emotion v10, @emotion/react is not necessary. Similarly, @emotion/styled is only required if you intend to use Styled Components (e.g.

const Foo = styled("div")`font-size: 14px;`;
).

Answer №2

Make sure to delete the @emotion/core package and then proceed with re-installing it using npm install. Once done, stick to the syntax provided in your code or consider this alternative approach:

import { css, jsx } from '@emotion/react'

const color = 'white'

render(
  <div
    css={css`
      padding: 32px;
      background-color: hotpink;
      font-size: 24px;
      border-radius: 4px;
      &:hover {
        color: ${color};
      }
    `}
  >
    Hover over to see the color change.
  </div>
)

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

Can you help me properly position a scroll bar in the lower-right corner of the div?

I am in the process of designing a web page layout with three sections - left, top right, and bottom right. The left and bottom right sections should have scrollbars, and the entire page should fill the screen. I am utilizing Bootstrap 4 for this project. ...

Position the slideshow within the parent DIV element

I'm facing a challenge trying to incorporate a carousel within the boundaries of this div. I experimented with using z-index on the carousel, which did solve the issue but it rendered all other items within it unclickable. I also attempted setting th ...

Modify the navbar background color in bootstrap-vuejs

As per the information provided in the documentation, the instructions for setting styles for the navigation bar are as follows: <b-navbar toggleable="lg" type="dark" variant="info"> <!-- Or --> <b-navbar toggleable="lg" type="dark" variant ...

Issue: The module `./index` cannot be located in the directory ` ode_modules eact-native/scripts/.`

After attempting to upgrade my React Native, I encountered this error message: The error states: Error: Unable to resolve module ./index from \node_modules\react-native\scripts/.: The module ./index could not be found from \node ...

Adjust the image to stretch and crop appropriately to perfectly fit the specified dimensions

I have a div with an image inside of it, and the overflow of the div is set to hidden so that the image will be cropped if it exceeds the width or height. It was working correctly, but sometimes it doesn't. What could be causing this issue? Here is th ...

Extending a universal design concept to a new theme within the MUI (Material UI) framework

I have implemented MUI's ThemeProvider and set up a Themes.js file. Within this file, I have defined the globalTheme for managing global styles such as typography and border-radius. My intention is to extend the properties of globalTheme to both light ...

Using CSS to customize the appearance of the title within an image tag

Having some trouble styling the title within an image tag. I've looked at other questions but can't seem to get it working in my project. Unfortunately, I'm unable to make any modifications. Would appreciate a helping hand with this, plea ...

What is the reason for a newly created custom class to have priority over a class with a different name from an earlier loaded file?

Utilizing Bootstrap 4 from the CDN, I have created a new class named: .pt-10 { padding-top: 10rem !important; } The css files are loaded in the following order within the html document: <link href="bootstrap cdn css..."> <link href="my file"> ...

In what way does Vans offer a compact JSON 3D model?

I'm working on building a 3D website using Three.js, React, and react-three-fiber for customizing shirts. While exploring Vans' customization page, I noticed that they achieve high-quality 3D visuals with small JSON files instead of glTF. My glTF ...

Is there a way to utilize regular expressions in React to dynamically insert onclick events into specific words within a text block?

I've been experimenting with regular expressions in React to implement an onclick event for each word in a text. I've attempted two different strategies, but neither has been successful thus far. Initially, I tried: return( <div> & ...

Customizing the design of vuetify table header to your liking

My goal is to implement a custom style for v-data table headers using the fixHeader method. The CSS code is intended to keep the header fixed in place even when scrolling horizontally. The issue arises as the style is applied to the inner <span> ele ...

Grab the current URL using useRouter in a Next.js app

I am using userouter to obtain the URL of the current page and then utilizing the clipboard to copy it. However, I am encountering an issue where the copied content shows as object object instead of the expected URL. Can someone please help me identify w ...

Ways to attach an item using its lower point instead of its upper coordinate

I am currently working on a poker table project using React. As of now, I have player components that need to be strategically positioned around the table. https://i.sstatic.net/gX9Ij.png One challenge I'm facing is that when the screen attribute ch ...

Is it possible to make consecutive axios requests?

I have a ReactJs application with two functions: AuthService.addUser(newUser); AuthService.userCategories(usercategories); When the submit button is clicked, I want to run these functions separately. The Axios request of the second function should be ca ...

Tips for updating the styleurls link dynamically by clicking a button and maintaining the changes

In the midst of developing an angular 2 project, I am currently working on integrating theme settings. Within my project, I have crafted three distinct component.scss files: red.component.scss, yellow.component.scss, and blue.component.scss. My goal is t ...

Text will be positioned between the Header and Footer

The text positioned between the header and footer elements is flowing beneath them. I attempted to use flex-start and flex-end on both the header and footer, but they do not align to the absolute top. Instead, they are positioned above and below the conten ...

What is the solution to adding values from a counter?

I am trying to create a JavaScript counter: function animateSun() { var $elie = $("#sun"); $({ degree: 0 }).animate({ degree: 360 }, { duration: 190999, easing: 'linear', step: function(val) { now = Math.round ...

Issue: Exceeding rendering limit. React has a restriction on the number of renders to avoid endless loops

I'm in the process of creating a basic to do list using React, and I've encountered an issue with the handleSubmit() function that I can't seem to resolve. import React, { useState } from "react"; function TaskList() { const [ta ...

"Implementing a nested drawer component in ReactJS with the Material-

Exploring Reactjs with material ui and in search of a nested drawer component. Check out this example: Nested Drawer I've noticed existing drawer components on the material ui site, but none fit my specific requirements. Can anyone guide me on how t ...

Adding a translucent overlay on top of a background image within a div, while keeping the text on top

Here is the current code that I am working with: <div class="square"> <div id="wrapper"> <h2>Text</h2> <h3>Text</h3> </div> </div> .square { padding: 10px; width: 150px; height ...