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.

Pass SVG in as a variable

See original GitHub issue

I 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:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jhamletcommented, Oct 5, 2015

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:

// file: src/icons.js
module.exports = {
    Foo: require('babel!svg-react!../files/foo.svg'),
    Bar: require('babel!svg-react!../files/bar.svg'),
    Baz: require('babel!svg-react!../files/baz.svg'),
    // and so on ...
};

Then consumers can use it like so:

// file: index.js
var React = require('react');
var IconFoo = require('./src/icons').Foo;

module.exports = React.createClass({
    render: function () {
        return <IconFoo className="foo" />;
    }
});

Alternatively, you can try the following.

This uses require.context in webpack to do something more dynamic.

// file: src/icons.js
var React = require('react');
var context = require.context('babel!svg-react!../files', false, /\.svg$/);

module.exports = React.createClass({
    render: function () {
        // We use './' since context is relative to '../files'
        var path = './' + this.props.iconName + '.svg';

        var element;

        try {
            element = React.createElement(context(path), this.props);
        }
        catch (error) {
            element = <span {...this.props} className='missingIcon' />;
        }

        return element;
    }
});

Then other’s can do the following:

// file: index.js
var React = require('react');
var Icon = require('./src/icons');

module.exports = React.createClass({
    render: function () {
        return <Icon iconName='foo' />;
    }
});
0reactions
tonydiazcommented, Oct 5, 2015

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

var SVG = require("./svg.js")
var Comp = React.createClass({
    render() { 
      //Load some stuff
      <SVG name="play.svg" className="button" />
    }

svg.js

var svgPath = "../files/svg/";

var SVG = React.createClass({
    render() {
        //I'm unable to get the line below to work. It seems the `require` doesn't like it 
        //when I concatenate a string
        var Icon = require("babel!svg-react!" + svgPath + this.props.filename);
        return <Icon className={this.props.className}/>
    }
});

Read more comments on GitHub >

github_iconTop 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 >

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