The import error occurred because the module 'styled-components' does not export 'createGlobalStyle'

I am encountering an issue while attempting to import createGlobalStyle from styled-components. Despite having installed the styled-components npm package with version @3.4.10, it doesn't seem to work as expected.


const GlobalStyle = createGlobalStyle`
html {
    height: 100%
}
* {
    padding: 0;
    margin: 0
}

`
export default GlobalStyle

The snippet above shows where I am trying to import createGlobalStyle.

import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import GlobalStyle from './theme/globalStyles'

ReactDOM.render(
  <React.StrictMode>
    <App />
    <GlobalStyle />
  </React.StrictMode>,
  document.getElementById('root')
)

This is the index.js file where I make use of the GlobalStyle component.

Upon running the code, I encountered the following error:

./src/theme/globalStyles.js
Attempted import error: 'createGlobalStyle' is not exported from 'styled-components'.

If anyone could offer some guidance or pointers on this matter, it would be greatly appreciated.

Answer №1

If you are utilizing version 3.4.10 of styled-components, then it is necessary to use injectGlobal instead of createGlobalStyle, as the latter was introduced only in v4 of styled-components. To learn more, visit: [Deprecated] injectGlobal

To ensure your code functions correctly, you will need to make a few modifications:

import { injectGlobal } from 'styled-components';

const GlobalStyle = injectGlobal`
html {
    height: 100%
}
* {
    padding: 0;
    margin: 0
}
`

export default GlobalStyle

In your index.ts:

import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import GlobalStyle from './theme/globalStyles'

ReactDOM.render(
  <React.StrictMode>
    <GlobalStyle /> // this should be placed first
    <App />
  </React.StrictMode>,
  document.getElementById('root')
)

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

Tips for iterating over the DOM in React and toggling the corresponding class when an ID is clicked

I'm currently transitioning from jQuery to React and I need assistance with the following code snippet. I am trying to figure out how to iterate through my menu items, and when an element with the id #vans-by-manufacturer is clicked, locate an element ...

I am facing an issue in my Vue Project where the CSS does not recognize URLs enclosed in quotes

When attempting to pass over a quoted URL to background-image or cursor, I'm facing an issue where the file simply doesn't load. I am currently working with Vue and have installed the following libraries: Vuetify, SASS, SASS-Loader, and Node-S ...

Image loading in NextJS is limited to only Firefox browsers

Images are loading only in Firefox and not anywhere else. Here is the import statement: import gradient from '../public/mountains/gradient.svg' And here is the Image component: <div id='gradient' className={`${styles.bgimage} ${sty ...

Achieving vertical alignment for multiple rows in an AppBar in Material-UI: A guide

Struggling to vertically center multiple items/rows in a Material-UI AppBar/ToolBar? Here's the issue... Here is my current setup... https://i.stack.imgur.com/7IUY4m.png And here is what I'm aiming for... https://i.stack.imgur.com/y3UJ1m.png ...

Using Next.js in conjunction with Tailwind CSS and the shadcn/ui library to tackle the issue of preventing overscroll from creating

As I delve into a Next.js project, my goal is to use Tailwind CSS to craft an interface featuring a sidebar on the left, a header at the top, and a main content area. However, an issue has surfaced where users can over-scroll, leading to a blank space appe ...

Maximizing Performance: Implementing Debounce Hooks in React

import { useEffect, useState } from 'react'; export default function implementDebounce(text: string, delay: number) { const [value, setValue] = useState(''); useEffect(() => { const timerId = setTimeout(() => { set ...

What is the most effective approach for importing scss variables when using React Styled-Components?

When working with normal React components, it's common to import other scss files to access variables like $color-primary: blue. However, what's the best way to achieve this with styled-components? Let's say I have some main variables for t ...

Sharing precise API information between React components in React Components

I am currently learning react and facing an issue with sending data to other component files. I am trying to verify a user login from a backend API within an if statement, and if successful, I want to send the user ID to another component file. However, ...

"Disabling Click Event on Sidebar Menu in Angular: A Step-by-Step Guide

I am working on an Angular project with a sidebar that I want to modify in order to disable click events on the menu items. My goal is to guide users through a specific flow without allowing them to navigate freely. However, simply disabling the [routerLin ...

Creating an eye-catching loading animation for page content using jQuery and CSS

Many websites incorporate animations to enhance user experience when content loads in the background. LinkedIn is one example of a site that features a nice animation during content loading. Below is a screenshot for reference, showcasing the type of anima ...

The el-dialog is functioning properly, however, I am looking to add CSS that will animate it to open

I've set up a dialog to open on button click, and it's functioning correctly. My goal is to have the dialog open from the bottom with a height of 300px, show the contents inside it, and then hide when I click outside or inside any element. Here ...

User authentication status is only unavailable within the component when redirection occurs

I have successfully implemented my dasboard component, which displays the user's auth.uid and projects fetched from firestore. However, there is a line //if (!auth.uid) return <Redirect to='/signin'/> that causes issues when uncommente ...

Error encountered: _moment2.default.date is not a recognized function within the React application

I keep encountering an issue when attempting to utilize a date from Moment.js in the constructor, componentWillMount, or componentDidMount. The error message I receive is: Uncaught TypeError: _moment2.default.date is not a function It's worth noting ...

"Troubleshooting the issue with the jQuery function not being able to trigger a

When I press the enter key in an input field, I want to be able to select options from a searchable dropdown menu. However, this functionality is not working and the alert message I set up is also not functioning as expected. <div class="form-group m-f ...

Is there a way to implement CSS stylesheets on a particular component?

I am currently working on a project that involves developing an Android app and a web app within the same Next.js project. I am trying to ensure that the recommended Ionic global stylesheets do not affect the web layout. Within my server component app/lay ...

CORS policy is preventing all development requests from going through

In my setup, I have a Rails API paired with a React client side. Everything has been running smoothly for some time now, but recently I encountered an error while working on it: Access to XMLHttpRequest at 'http://localhost:3000/api/v1/user/authed&apo ...

Move the <div></div> element to the bottom of the webpage and center it

My HTML includes a <div>...</div> section that serves as a toolbar. Is there a method to position this section at the bottom of the webpage (document, not viewport) and align it to the center? ...

Utilizing React Formik to implement conditional select options based on the value of

Currently, I am faced with a problem while using react formik. I am working on a form that includes a checkbox field. When this field is checked, a select box with two options - Free and Paid - should be displayed. If the user selects Paid, another select ...

Overlap of a fixed right column over a fixed sidebar in Bootstrap 4, causing it to overlap the

I am struggling to fix the layout of my website's right dark column and sidebar. I've attempted several methods, including setting fixed parameters with no success. My goal is to have the content-c class (dark column) fixed and the sidebar fixed ...

Determine if the grid is accurately sorted in sequence using Cypress

I am currently working on sorting images based on their order, with the options for new and old. The [data-cy="list-file"] wraps all the images, while [data-cy=fileCard-list] represents each individual image. After selecting the 'old' b ...