Setting <Image /> source with string concatenation fails ('Requiring unknown module...')
See original GitHub issueI’ve seen this mentioned before: https://github.com/facebook/react-native/issues/282#issuecomment-96908438
I’m trying to include an image from within the project, the name of which is dependant on variables.
var imageName = `image!${var1}_of_${var2}`
contents = (
<Image source={require(imageName)} style={styles.card} onPress={this._onPress} />
)
The above fails with the usual:
Requiring unknown module “image!somwething_for_somethingelse”. If you are sure the module is there, try restarting the packager.
These also fail in the same manner:
<Image source={require(
image!${var1}of${var2})}>
<Image source={require('image!' + {var1} + '_of_' + {var2})} >
This works, no problem:
<Image source={require('image!somwething_for_somethingelse')}>
I’ve also tried
- Stop and re-build Xcode project
- Restarting the packager
Issue Analytics
- State:
- Created 8 years ago
- Reactions:20
- Comments:31 (6 by maintainers)
Top Results From Across the Web
React Native Image component, requiring unknown module
The answer is that you need to explicitly require all images or else they will not be bundled, hence the unknown module error....
Read more >Output - webpack
The top-level output key contains a set of options instructing webpack on how and where it should output your bundles, assets, and anything...
Read more >IO tools (text, CSV, HDF5, …) — pandas 1.5.2 documentation
If #1 fails, date_parser is called with all the columns concatenated row-wise into a single ... quoting : Set quoting rules as in...
Read more >Content Configuration - Tailwind CSS
This guide covers everything you need to know to make sure Tailwind generates all of the CSS needed for your project. . Configuring...
Read more >mod_ssl - Apache HTTP Server Version 2.4
This module provides SSL v3 and TLS v1.x support for the Apache HTTP Server. ... Such a file is simply the concatenation of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top 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
this ‘feature’ is retarded and is annoying. Would very much be interested how i can bypass this stupid check
WHILE i agree that it is a good idea to stop developers from including images which consist of concatenated strings, it is the developer’s responsibility to code in such way that won’t cause him trouble in the future. A much better solution for react-native would be to print a notice to the console alerting them of what they just done.
We intentionally don’t support dynamically generated image names because it makes it very hard to manage assets over time, just like how you wouldn’t want to do
var MyComponent = require(‘./’ + libName + typeOfComponent);
or something like that. Dynamic image name generation like this also won’t work with the new dynamic asset managing system we’re rolling out which let’s you add and update assets on the fly with just cmd+R (no more need to add to Xcode and recompile).