Semi dynamic import
See original GitHub issueSorry if this is documented somewhere in an Issue but I can’t seem to find it.
I’m trying to do something like this and get an Invariant Violation.
const ListItem = ({ icon: Icon }) => (
<View>
<Icon />
<View>
);
<ListItem icon={require('../path/to/yo-that.svg')} />
I’m also open to other ways of doing this as well that allow for a smidge of code reuse.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Semi-Dynamic Import or readfile with JavaScript on localhost ...
I need to import X files containing a single variable into a larger application, where X is specified by the user.
Read more >Dynamic import() - V8 JavaScript engine
Dynamic import () introduces a new function-like form of import that unlocks new capabilities compared to static import .
Read more >Generic semi-dynamic page component in Next.js? : r/webdev
So I am building a e-commerce site and I am using Next. Every category page is basically the same and to make the...
Read more >What are the cons of dynamic imports a.k.a code splitting ...
What are the cons of dynamic imports a.k.a code splitting JavaScript files? ... If you ignore the week and a half or orientation...
Read more >@babel/plugin-proposal-dynamic-import - Package Manager
@babel/plugin-proposal-dynamic-import. owner babel68.7mMIT7.18.6. Transform import() expressions. babel-plugin. readme.
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
@spacesuitdiver I’m importing svg files using
require
, and it only works if I usedefault
attribute of the object returned byrequired
call.So I think what you need is
<ListItem icon={require('../path/to/yo-that.svg').default} />
I got it working with the following flow type definition and jest mock.
flow-typed/svg.js
.flowconfig
__mocks__/svg.js
Now it works with both
import ... from '...'
andrequire('...').default
.