Use with a local file

前端 未结 10 819
醉梦人生
醉梦人生 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:18

    If loading images dynamically one can create a .js file like following and do require in it.

    export const data = [
      {
        id: "1",
        text: "blablabla1",
        imageLink: require('../assets/first-image.png')
      },
      {
        id: "2",
        text: "blablabla2",
        imageLink: require('../assets/second-image.png')
      }
    ]
    

    In your component .js file

    import {data} from './js-u-created-above';
    ...
    
    function UsageExample({item}) {
       <View>
         <Image style={...} source={item.imageLink} /> 
       </View>
    }
    
    function ComponentName() {
       const elements = data.map(item => <UsageExample key={item.id} item={item}/> );
       return (...);
    }
    
    0 讨论(0)
  • 2021-02-01 02:19

    This from https://github.com/facebook/react-native/issues/282 worked for me:

    adekbadek commented on Nov 11, 2015 It should be mentioned that you don't have to put the images in Images.xcassets - you just put them in the project root and then just require('./myimage.png') as @anback wrote Look at this SO answer and the pull it references

    0 讨论(0)
  • 2021-02-01 02:20

    To display image from local folder, you need to write down code:

     <Image source={require('../assets/self.png')}/>
    

    Here I have put my image in asset folder.

    0 讨论(0)
  • 2021-02-01 02:22

    I was having trouble with react-native-navigation, I created my own header component, then inserted a image - as logo - on the left before title, then when I was triggering navigate to another screen and then back again, logo was loading again, with a timeout near 1s, my file were local. My solution :

    Logo.json

    {"file" : "base64 big string"}
    

    App.js

    import Logo from '.../Logo.json'
    ...
    <Image source:{{uri:Logo.file}} />
    
    0 讨论(0)
提交回复
热议问题