1

Let's say I have a React.js component called CountryFlag. I also have a directory of SVG images for each country's flag at countries/

This component takes a single prop - countryCode. This would be something such as ES for Spain.

How can I use the ES6 import construct with this.props.countryCode when this.props is not in scope?

I have tried to do this in componentDidMount() but get a syntax error:

Syntax error: 'import' and 'export' may only appear at the top level (6:2)
4
  • You wouldn't do this, you just reference their url and host the images on a CDN Commented Feb 21, 2017 at 11:54
  • You can't import something based on a property (as far as I know). Would something like this help? : github.com/matthewwithanm/react-inlinesvg you could import the Isvg module in the component, then have the Isvg src as the property value <Isvg src={this.props.svgImage}/> Commented Feb 21, 2017 at 11:56
  • @CallumLinington: I will be doing but I'm bundling it for production and need to do this using Webpack. Commented Feb 21, 2017 at 12:27
  • @Jayce444: Thank you! Commented Feb 21, 2017 at 12:27

3 Answers 3

2

You should use require. like:

class Whatever extends Component{
  constructor(props) {
    super(props);
    this.countryImage = null;
  }
  componentDidMount() {
    const { countryCode } = this.props;
    this.countryImage = require(`./countries/${countryCode}.svg`); 
  }
  render() {
    return (
      <img src={this.countryImage} alt="Whatever" />
    );
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can make use of System.import to import a component dynamically in componentDidMount

constructor(props) {
  super(props)

  this.state = { component: null }
}
componentDidMount() {
    this.loadComponent(component => this.setState({ component }));
}
loadComponent = callback => {
    let component = null;
    // This needs to be relative to the current module otherwise Webpack won't know where to look
    System.import(`./countries/${this.props.countryCode}`).then(module => callback(component = module))
    return component;
}

4 Comments

Thank you! Will this allow Webpack to bundle the SVG?
Why exactly import and not require? The former will result in a new chunk, which may or may not be the desirable behaviour.
Well I am not sure, but it should help you bundle the svg as well. You can try and see.
System.import is deprecated and has been replaced with import() (the function)
1

You can also use your props on src to your image. Something like <img src={"./img/" + this.props.country + ".svg"}>

2 Comments

Wouldn't this prevent Webpack from bundling the SVG though?
Really not sure, but i hadn't problems with this. Just with write right path to images)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.