question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Lazy loading styled-components component triggers warning message incorrectly

See original GitHub issue

Reproduction

https://github.com/TLadd/preact-styled-components-lazy-load-warning. The relevant files are

preact@10.4.4

Steps to reproduce

Load the app and observe the following warning message is printed:

The component styled.div with the id of ā€œsc-AxjAmā€ has been created dynamically. You may see this warning because youā€™ve called styled inside another component. To resolve this only create new StyledComponents outside of any render method and function component.

This warning occurs only when lazy loading a styled component or lazy loading something that renders a styled component with preact. The warning message is not printed if a dynamic import is not used or if using React instead of preact.

Looking at styled-components, itā€™s triggering this warning message if calling useRef doesnā€™t throw when declaring the component.

Expected Behavior

The warning message should not be printed.

Actual Behavior

The warning message is printed.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:27 (13 by maintainers)

github_iconTop GitHub Comments

6reactions
kittencommented, Jun 15, 2020

Hiya šŸ‘‹

Iā€™ve put in the warning and it triggers when we a hook doesnā€™t throw during StyledComponent creation. Iā€™m not sure what context this permissiv behaviour happens in, but it does seem like a tiny issue in preact/compat to me as during the time of import we shouldnā€™t be inside any component context; that being said itā€™s safe to ignore.

The reason why Iā€™ve proposed the change and weā€™ve added it is that this is still a common mistake when styled-components are used. We get a lot of issues that relate to dynamically created components: unexpected edge cases that are usually impossible, memory leaks on the client-side and in SSR, out of memory errors, rerender bugs triggered by this, etc.

After having seen probably hundreds of these issues and comments, itā€™s clear that once people create more than a thousand components, telling them to track down the few cases where theyā€™re dynamically created (and explaining this to beginners) is a daunting ask.

Hence this warning was introduced to rule this mistake out entirely, which has mostly eliminated this category of issues entirely šŸ˜…

5reactions
sventschuicommented, Oct 12, 2020

@heymartinadams that ā€œtotal hackā€ how you call it seems a bit off to me. Adding the react-refresh runtime manually doesnā€™t feel right. @JoviDeCroock spent quite some time to create prefresh - the react-refresh alternative for preact. It even features a next.js plugin (https://github.com/JoviDeCroock/prefresh/tree/main/packages/next).

As @developit mentioned I maintain a next.js plugin that will take care of the aliasing of react to preact and also includes prefresh: https://github.com/sventschui/next-plugin-preact . I guess this plugin would be your best bet for a stable setup. If you experience any issues with that and styled-components it would be helpful if you could share a small reproduction project where we could take a closer look.

Read more comments on GitHub >

github_iconTop Results From Across the Web

lazy loading for image in styled component - Stack Overflow
I use div in style component, but it has flickering issue waiting for the image to come in. Is there any lazy loading...
Read more >
Releases - styled-components
Transient props are a new pattern to pass props that are explicitly consumed only by styled components and are not meant to be...
Read more >
How To Handle Async Data Loading, Lazy Loading, and Code ...
In this step, you asynchronously loaded components. You used lazy and Suspense to dynamically import components and to show a loading message ......
Read more >
Suspense for Data Fetching (Experimental) - React
The rest of this page includes content that's stale, broken, or incorrect. React 16.6 added a <Suspense> component that lets you ā€œwaitā€ for...
Read more >
Frequently Asked Questions - Material UI - MUI
Possible reasons Ā· You have another @mui/styles library somewhere in your dependencies. Ā· You have a monorepo structure for your project (e.g, lerna,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found