Pass SVG in as a variable
See original GitHub issueI want to be able to pass an svg
name into this react component and call it from the render function. Is there currently a way to do this?
Something like this:
var React = require("react");
var Icon = require("babel!svg-react!");
var SVG = React.createClass({
render: function() {
return <Icon path="../node_modules/logo.svg" />
}
});
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How do I define or reference a variable in SVG? - Stack Overflow
You can define some things such as gradients, masks, patterns and filters relative to the object they are being applied to. You can...
Read more >SVG Referenced Parameter Variables 1.0, Part 1: Primer
Using the <object> element, you can pass parameters by the use of child <param> elements. Each <param> element should have name/value pairs with...
Read more >Referenced Parameter Variables in SVG
Using the <object> element, you can pass parameters by the use of child <param> elements. Each <param> element should have name/value pairs with...
Read more >CSS Variables and SVG <use> Elements - CodePen
Now supported in Firefox (31+), CSS Variables will allow SVG icons to be infinitely customizable! The first icon defines the graphic, the remainder...
Read more >SVG SET ATTRIBUTE
SVG SET ATTRIBUTE ( {* ;} pictureObject ; element_ID ; attribName ; attribValue ... Therefore, you pass a variable (object variable only) or...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The issue you describe seems to be a limitation of webpack.
If you wish to reduce the ‘path’ that consumers of these svgs need to know about, can I suggest trying it this way:
Then consumers can use it like so:
Alternatively, you can try the following.
This uses
require.context
in webpack to do something more dynamic.Then other’s can do the following:
Thanks. The use-case is a reusable component so when an SVG is loaded the caller will only need to call the name of the SVG and not need to fill out the entire path each time.
I have a problem concatenating a string within a require statement. Do you think it is a
async
issue?Caller
svg.js