I'm going to import a bunch of SVG illustrations into my project as inline SVG components using the new feature of create-react-app @2.0.0.
Here is my svg-import.js:
import React from 'react';
import { ReactComponent as StartLogo } from '../images/start/logo.svg';
import { ReactComponent as StartTitle } from '../images/start/title.svg';
...
import { ReactComponent as IntroAvatar } from '../images/intro/avatar.svg';
import { ReactComponent as IntroDialog } from '../images/intro/dialog.svg';
...
const svgIllustrations = {
"start": {
"logo": <StartLogo className="svg svg-start-logo"/>,
"title": <StartTitle className="svg svg-start-title"/>,
...
},
"intro": {
"avatar": <IntroAvatar className="svg svg-intro-avatar"/>,
"dialog": <IntroDialog className="svg svg-intro-dialog"/>,
...
}
}
export default svgIllustrations;
When I want to use them:
import svgIllustrations from '../svg-import.js';
...
render() {
return (
{svgIllustrations.start["logo"]}
)
}
The code works.
However, since there're lots of SVG files to be imported and all of them are named and structured under a certain rule, I'm wondering if there's a better way to do something like this.