Skip to main content
Solved

.loadCSSFromString() generating redundant style elements?

  • December 15, 2020
  • 3 replies
  • 29 views

Kamille_Parks11
Forum|alt.badge.img+27

I’ve noticed that when using .loadCSSFromString() the frontend renders multiple duplicates of the same <style>/<style> element in the DOM body. Is there a way around this that I’m missing? In the example below, .loadCSSFromString() is placed at top-level and not within a loop.

Best answer by Stephen_Suen

Update: I noticed its creating a new <style> everytime State gets updated. Still interested if there’s a way to load the css only once since we can’t use Webpack or some other compiler.


Are you calling loadCSSFromString inside a React component? Assuming the styles don’t need to change, you could move this outside of the component, e.g. call it once at the top of your index file.

3 replies

Kamille_Parks11
Forum|alt.badge.img+27
  • Author
  • Brainy
  • 2679 replies
  • December 15, 2020

Update: I noticed its creating a new <style> everytime State gets updated. Still interested if there’s a way to load the css only once since we can’t use Webpack or some other compiler.


Stephen_Suen
Forum|alt.badge.img+19
  • Employee
  • 83 replies
  • Answer
  • December 15, 2020

Update: I noticed its creating a new <style> everytime State gets updated. Still interested if there’s a way to load the css only once since we can’t use Webpack or some other compiler.


Are you calling loadCSSFromString inside a React component? Assuming the styles don’t need to change, you could move this outside of the component, e.g. call it once at the top of your index file.


Kamille_Parks11
Forum|alt.badge.img+27
  • Author
  • Brainy
  • 2679 replies
  • December 15, 2020

Are you calling loadCSSFromString inside a React component? Assuming the styles don’t need to change, you could move this outside of the component, e.g. call it once at the top of your index file.


Yep, you’re right. Could have sworn I put it at top level but I see now its within a component. Thanks Stephen!