Search code examples
imagereact-nativeabsolute-path

React Native absolute image path


I have images in my app at appname/assets/<> and would like to refer to them with an absolute path since I may use them with multiple components in multiple relative locations. I know how to use {require('../assets/image.png')}, but I want to save the absolute path of the image as a string in a database to reference in many places. I just can't tell what to add ahead of "/assets/image.png" to create this absolute path. I've tried a couple of versions of "appname/assets/image.png" but I just can't figure it out. What does the full React Native file tree look like for an app? Thanks for your help.


Solution

  • #1- Babel Root Import

    Babel plugin to change the behaviour of import to root based paths:

    https://github.com/mantrajs/babel-root-slash-import


    #2- Package.json

    https://github.com/facebook/react-native/issues/3099#issuecomment-221815006