<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Fernando De Freitas</title>
    <description>The latest articles on DEV Community by Fernando De Freitas (@fdefreitas).</description>
    <link>https://dev.to/fdefreitas</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F355254%2F61eb26ed-3d94-4700-afa6-da77ee5cb8fa.jpg</url>
      <title>DEV Community: Fernando De Freitas</title>
      <link>https://dev.to/fdefreitas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fdefreitas"/>
    <language>en</language>
    <item>
      <title>How to obtain a URI for an image asset in React Native (With Expo)</title>
      <dc:creator>Fernando De Freitas</dc:creator>
      <pubDate>Fri, 22 May 2020 22:13:58 +0000</pubDate>
      <link>https://dev.to/fdefreitas/how-to-obtain-a-uri-for-an-image-asset-in-react-native-with-expo-7bm</link>
      <guid>https://dev.to/fdefreitas/how-to-obtain-a-uri-for-an-image-asset-in-react-native-with-expo-7bm</guid>
      <description>&lt;p&gt;Another way of importing and handling image assets without using &lt;code&gt;require()&lt;/code&gt;, no third-party plugins required.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I’m currently trying to master React (Native) so this is probably a horrible hack. I’ll be updating this post as I find better ways of doing this.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One of the issues I’ve found while trying to build the proof of concept for an app is that you need to "pre-populate" it with some dummy data just to test stuff out quickly.&lt;/p&gt;

&lt;p&gt;It’s all good and well for strings, numbers, objects, etc. But then if you need to display images it gets a bit tricky.&lt;/p&gt;

&lt;p&gt;Assuming you don’t want to use image from an external source, the quickest way to have pre-existing images when testing in a simulator or a physical device is to load them in your project’s &lt;code&gt;assets/&lt;/code&gt; directory (or at least that’s the best solution I’ve found).&lt;/p&gt;

&lt;h1&gt;
  
  
  The problem
&lt;/h1&gt;

&lt;p&gt;After reading Expo’s documentation it initially seemed like the only way to "import" an image file in your code was by using require(). Now, this being 2020 I thought, surely, there must be a better way.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Full disclosure: When I try to use require() in my project I get a compiler error saying "require is not defined". I’m not sure if I’ve caused it by migrating my project to Typescript.&lt;/p&gt;

&lt;p&gt;Apparently it’s a Metro/Webpack config issue but I haven’t been able to figure out how to fix it. If you, reader, happen to know how to fix it please drop me a message, thanks!&lt;/p&gt;

&lt;p&gt;Now let’s keep going.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  The other way
&lt;/h1&gt;

&lt;p&gt;Turns out there’s another way, maybe writing a one more line of code, but using purely ESModules, the future!&lt;/p&gt;

&lt;p&gt;First you need to import your image like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;exampleImage&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./assets/images/example.png&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;If you were to &lt;code&gt;console.log(exampleImage)&lt;/code&gt; you’d get a number printed, like &lt;code&gt;1&lt;/code&gt;, or &lt;code&gt;8&lt;/code&gt;. I assume this is just an id or mapped value, however you prefer to call it, generated by Metro’s asset loader.&lt;/p&gt;



&lt;p&gt;Now, the important bit:&lt;/p&gt;

&lt;p&gt;React Native’s official Image Component provides us with a method called &lt;a href="https://reactnative.dev/docs/image#resolveassetsource" rel="noopener noreferrer"&gt;resolveAssetSource()&lt;/a&gt;. This method takes a "number" (related to what I’ve mentioned above) or an &lt;code&gt;ImageSource&lt;/code&gt; object as its only parameter and returns an object with &lt;code&gt;width&lt;/code&gt;, &lt;code&gt;height&lt;/code&gt;, &lt;code&gt;scale&lt;/code&gt; and &lt;code&gt;uri&lt;/code&gt; properties, this last one being the one we care about in this case. This will look like:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;exampleImageUri&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolveAssetSource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;exampleImage&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;uri&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Now, what’s the point of all this you ask. Well, to be honest you could always use &lt;code&gt;require()&lt;/code&gt; and it would work and that’s it right? Well not exactly.&lt;/p&gt;
&lt;h2&gt;
  
  
  The problem with using require() for image assets
&lt;/h2&gt;

&lt;p&gt;Using &lt;code&gt;require()&lt;/code&gt; has a couple of caveats. First, it’s CommonJS and we’re working on the browser-side of things, where it doesn’t really belong, in the end you’re using just a synthetic &lt;code&gt;require()&lt;/code&gt; being provided by the bundler.&lt;/p&gt;

&lt;p&gt;Second, we’d have a mixture of ESModule &lt;code&gt;import&lt;/code&gt;s and CommonJS &lt;code&gt;require()&lt;/code&gt; in our codebase. Something we can easily avoid it, at least in this case.&lt;/p&gt;

&lt;p&gt;Third, the second to most important in my opinion. The syntax is different when you want to display your image in an &lt;code&gt;&amp;lt;Image&amp;gt;&lt;/code&gt; component.&lt;/p&gt;
&lt;h3&gt;
  
  
  require (CommonJS)
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;exampleImage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./assets/images/example.png&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;exampleImage&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  import (ESModule)
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;exampleImage&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./assets/images/example.png&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;exampleImageUri&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolveAssetSource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;exampleImage&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;uri&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="na"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;exampleImageUri&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Bonus (Dynamic imports)
&lt;/h3&gt;

&lt;p&gt;I know it’s also possible for &lt;code&gt;require()&lt;/code&gt;, but you could even use dynamic imports, like:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;exampleImage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./assets/images/example.png&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;exampleImageUri&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolveAssetSource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;exampleImage&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;uri&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;And lastly, by using import you’ll provide the source attribute to the &lt;code&gt;&amp;lt;Image/&amp;gt;&lt;/code&gt; component in the same format you’ll need to provide it when you eventually finish your POC and start working with real data, fetching images from external URLs like CDNs and the like, that source format is and object with a &lt;code&gt;uri&lt;/code&gt; property, like:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;exampleImageUri&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In conclusion, this way you have a consistent way of handling image assets. All the code and components you wrote to handle and display your images when getting started building your app will still be useful once you switch from dummy to real data from external sources. No need to refactor components and update source props and the like.&lt;/p&gt;

&lt;p&gt;Anyways, I hope this is useful to you, I’m still trying to master React (Native), this post is one part note-taking for myself and one part writing it down so somebody having the same issues can find it. If something I’ve stated in this post is wrong feel free to leave a note and I’ll try to get it corrected.&lt;/p&gt;
&lt;h2&gt;
  
  
  A couple of things to know about the URI structure
&lt;/h2&gt;

&lt;p&gt;This uri property is comprised of a normal looking URL, in the form of :&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

http://127.0.0.1:19001/assets/assets/images/examples.png?platform=&amp;lt;android|ios...&amp;gt;&amp;amp;hash=&amp;lt;asset_hash&amp;gt;?platform=&amp;lt;android|ios...&amp;gt;&amp;amp;dev=true&amp;amp;minify=false&amp;amp;hot=false


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  A couple of things worth noting:
&lt;/h3&gt;

&lt;p&gt;There are two assets/ directories in the path, I’m assuming the top one relates to an internal directory for the bundler and the second one being part of the string path we’ve specified in our import statement example.&lt;/p&gt;

&lt;p&gt;For some reason it also has 2 sets of query strings. I’m assuming the second set being for the dev server and the first one some other internal Expo-related service.&lt;/p&gt;

&lt;p&gt;Now I’m not sure how this URI translates in production but, according to the &lt;a href="https://docs.expo.io/guides/assets/" rel="noopener noreferrer"&gt;Assets Guide&lt;/a&gt; in Expo’s official documentation, Expo uploads the project’s assets to Amazon CloudFront and I’m assuming creates some kind of map/replaces all assets references with the CDN URLs internally.&lt;/p&gt;
&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.expo.io/guides/assets/" rel="noopener noreferrer"&gt;Assets — Expo Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.expo.io/versions/v37.0.0/sdk/asset/" rel="noopener noreferrer"&gt;Asset SDK — Expo Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactnative.dev/docs/image" rel="noopener noreferrer"&gt;Image — Core Components — React Native&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import" rel="noopener noreferrer"&gt;import — Javascript | MDN&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Originally posted in:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/@fdefreitas/how-to-obtain-a-uri-for-an-image-asset-in-react-native-with-expo-88dfbe1023b8" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fv2%2Fresize%3Afill%3A88%3A88%2F1%2AGcFy_8mq2aWZlBVcOLKQmg.jpeg" alt="Fernando De Freitas"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/@fdefreitas/how-to-obtain-a-uri-for-an-image-asset-in-react-native-with-expo-88dfbe1023b8" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;How to obtain a URI for an image asset in React Native (With Expo) | by Fernando De Freitas | The Startup | Medium&lt;/h2&gt;
      &lt;h3&gt;Fernando De Freitas ・ &lt;time&gt;Jun 6, 2020&lt;/time&gt; ・ 
      &lt;div class="ltag__link__servicename"&gt;
        &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fmedium-f709f79cf29704f9f4c2a83f950b2964e95007a3e311b77f686915c71574fef2.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>reactnative</category>
      <category>expo</category>
    </item>
  </channel>
</rss>
