Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Rob Earlam
Rob Earlam

Posted on • Originally published atrobearlam.com on

Using Styled Components with NextJs

Sitecore Discover JS SDK

I've recently been working with a new JavaScript SDK for Sitecore Discover which is going to be released soon. This is a React library which provides a series of prebuilt components designed to enable people to up and running with Sitecore Discover as quickly as possible. You can read all about it on theSitecore Documentation Site.

Now the components come in a few different versions, we have straightJavaScript andTypeScript flavours. We also have bothstyled andunstyled versions of the components to give developers options in how they want to get started.

I began testing the SDK by integrating it withNextjs Commerce. I like this project for testing out JS SDK as its super quick and easy to get up and running, however I ran into some strange issues when using the styled versions of the components.

Styled Components with Nextjs

Now when using the styled versions of the components, the styles are provided byStyled Components – a library that uses tagged template literals to allow you write CSS to style your components easily.

Now when the page first loaded the styled components were rendered correctly, however whenever the Nextjs router fired to change the page content the styles were removed?

After some googling it turns out that you need to tell Nextjs that you’re using Styled Components in your application in order for this error not to happen.

To do so, you need to add the following configuration to your Next.config.js

compiler: {  styledComponents: true}
Enter fullscreen modeExit fullscreen mode

Once this was included everything worked well and my component styles persisted after a route change!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Developer, Movie Lover, Music Listener, Pizza Eater, Meat Smoker - Thoughts are my own - 🇬🇧 living in 🇦🇺
  • Location
    Melbourne
  • Work
    Head of Dev Advocacy @ Sitecore
  • Joined

More fromRob Earlam

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp