Use with a local file

前端 未结 10 820
醉梦人生
醉梦人生 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}) {
       
          
       
    }
    
    function ComponentName() {
       const elements = data.map(item =>  );
       return (...);
    }
    

提交回复
热议问题