Icons, placeholders and illustrations that ship inside your app are loaded with require, and the bundler packages each file along with your JavaScript. That route gives you two things a URL can't: React Native knows the image's size before it renders, and denser screens get a sharper file without any change to your code. Both depend on writing the require call in a form the bundler can read.

Key terms

  • Static image resource: an image file that lives in your source tree and is bundled with the app through require.
  • require('./file.png'): the call that bundles a local image and returns an opaque number you pass to the source prop of Image.
  • @2x and @3x suffixes: file name suffixes that mark the variants of one image made for higher screen densities.

require hands Image a bundled file

To show a local file, put it anywhere in your source tree and pass require with its path to source:

tsx
<Image source={require('./assets/empty-inbox.png')} />

The path resolves the way a JavaScript module path does, so ./assets/empty-inbox.png is looked up relative to the file of the component that calls require. What comes back is an opaque number that Image accepts as a source. React Native attaches some metadata to that source, but the metadata might change, so don't write code that reads it.

Only the images your code requires end up in the app package, and the bundler records each one's width and height. Because those dimensions are available as soon as the component mounts, a required image sizes itself: the element above needs no width or height. An explicit width or height in the style still overrides the recorded size. If you want the image to grow and shrink with flex instead, you may need to set {width: undefined, height: undefined} in its style, because the required source carries its own width and height.

On Windows, you might need to restart the bundler after you add new image files to the project.

One name, three files

Higher density variants sit next to the base file, with the suffix placed before the extension:

assets/
  badge.png
  badge@2x.png
  badge@3x.png

Your code keeps requiring ./assets/badge.png, with no suffix, and the bundler serves the variant that matches the device's screen density. The plain name is the 1x file. When React Native resolves the asset in JavaScript, it walks the available scales from smallest to largest and takes the first one that is at least the device scale. A screen denser than every variant, such as a 3.5 device with these three files, gets the largest one, @3x.

On iOS, the bundler copies each required image and its @2x and @3x variants into the app as loose files by default. Setting the RCTUseAssetCatalog key to true in the app's Info.plist compiles them into an asset catalog instead, and your require calls don't change.

Write every path out in full

require only works for images when the name passed to it is known statically. That rules out assembling the path while the app runs, for example from the state of a toggle:

tsx
// Wrong: the file name is only known at runtime
const name = muted ? 'volume-off' : 'volume-on';
<Image source={require('./icons/' + name + '.png')} />;

Move the decision outside require. Give each branch its own call with a complete literal path, then choose between the results. The MuteButton component below does this for a toggle:

tsx
const icon = muted
  ? require('./icons/volume-off.png')
  : require('./icons/volume-on.png');

icon now holds whichever source the condition picked, and <Image source={icon} /> needs no style, since both are bundled files whose size the bundler knows. The density variants keep working too: volume-off@2x.png and volume-off@3x.png next to volume-off.png are found from the same literal path.

The same require form also bundles files that aren't images, such as .mp3, .wav, .mp4, .mov, .html and .pdf.

Code Examples

tsx
import {Image, Pressable} from 'react-native';

type MuteButtonProps = {
  muted: boolean;
  onToggle: () => void;
};

// icons/ holds volume-on.png and volume-off.png, each with @2x and @3x variants
export default function MuteButton({muted, onToggle}: MuteButtonProps) {
  // Each require gets a complete literal path the bundler can resolve
  const icon = muted
    ? require('./icons/volume-off.png')
    : require('./icons/volume-on.png');

  return (
    <Pressable onPress={onToggle}>
      {/* No width or height needed: the bundler knows the image size */}
      <Image source={icon} />
    </Pressable>
  );
}
✓ Completed