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.

Semi dynamic import

See original GitHub issue

Sorry 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:open
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
buzbohdancommented, Sep 17, 2019

@spacesuitdiver I’m importing svg files using require, and it only works if I use default attribute of the object returned by required call.

So I think what you need is <ListItem icon={require('../path/to/yo-that.svg').default} />

0reactions
grit96commented, Dec 2, 2020

I got it working with the following flow type definition and jest mock.

flow-typed/svg.js

// @flow

declare module SvgImageStub {
  declare export default 'Svg';
}

.flowconfig

module.name_mapper='^[./a-zA-Z0-9$_-]+\.svg$' -> 'SvgImageStub'

__mocks__/svg.js

// @flow

const Svg = 'Svg';

export default Svg;

Now it works with both import ... from '...' and require('...').default.

Read more comments on GitHub >

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

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