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.

How to only inject symbols that are in use

See original GitHub issue

when using style-loader, the loaded modules determine the css.

However, with sprite-loader, the common use is to have an Icon component that renders a component based on a list of glyphs. This means that when you load the Icon component, all the glyphs get injected in the file.

I would like to know the best technique so that only active glyphs are loaded, specifically for React ≥v0.14.

How about a singleton <Sprites/> component that you put somewhere in your app, which renders the current <symbol/> set, and then instead of <use href… you use <Sprite id={…}/> which, behind the scenes, updates the Sprites element based on refcounting in constructor and componentWillUnmount.

Furthermore, since you can put the sprites at the bottom of the page (proof), I believe that will also solve server side rendering since it will be stringified after all the icons were referenced.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:17 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
radiovisualcommented, Aug 3, 2016

And yes, all the required svgs get added to the DOM, even when they are not in use.

This is a concern for me, because it is adding unused elements to the DOM. The projects I work on try to avoid dumping unused data to the DOM to keep the DOM/templates clean and lightweight. Also, in certain situations, SVG’s can come with security risks, so in a paranoid world: it’s best to limit SVG’s to only those you require.

I think that many of the ideas presented by @wmertens are really great, and I think providing a react-specific loader option is a cool idea, or breaking the react-ready component out to it’s own repo.

0reactions
kisenkacommented, Apr 28, 2017

New loader version adds ability to specify custom runtime generator. Please check default generator and custom generator implementation example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keyboard injection of symbols - Microsoft Q&A
Keyboard injection of symbols. How do I inject keyboard characters that are not alphabetical nor numerical, like the % or # ?
Read more >
Injecting objects into custom symbols, after deployment
I have a custom symbol that i have used tens of times on various displays. To extend the functionality, i need to inject...
Read more >
Injection operator - rlang
The injection operator !! injects a value or expression inside another expression. In other words, it modifies a piece of code before R...
Read more >
Composition API provide/inject with symbols are not working
Use the use function at your component and try to mount the component. Expected behaviour. Child component can inject the object from the...
Read more >
How inject a symbol through CMake command line
I need to do this from CMake command line invocation, not using add_definitions(), add_compile_definitions() or target_compile_definitions() ...
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