Use with a local file

前端 未结 10 962
醉梦人生
醉梦人生 2021-02-01 01:56

The documentation says that the only way to reference a static image is to use require.

But I\'m not sure where does react expect to have those images. The examples don

10条回答
  •  不要未来只要你来
    2021-02-01 02:01

    Using React Native 0.41 (in March 2017), targeting iOS, I just found it as easy as:

    
    

    The image file must exist in the same folder as the .js file requiring it.

    I didn't have to change anything in the XCode project. It just worked. Maybe things have changed a lot in 2 years!

    Note that if the filename has anything other than lower-case letters, or the path is anything more than "./", then for me, it started failing. Not sure what the restrictions are, but start simple and work forward.

    Hope this helps someone, as many other answers here seem overly complex and full of (naughty) off-site links.

    UPDATE: BTW - The official documentation for this is here: https://facebook.github.io/react-native/docs/images.html

提交回复
热议问题