Where to put images in a react-native project?

前端 未结 3 1468
天涯浪人
天涯浪人 2020-12-25 12:22

I am working on a react-native project and we are putting images currently in /images/ folder. Is it a good path for them ? Is there any best practice?

相关标签:
3条回答
  • 2020-12-25 12:55

    In image assets folder, create index.js file and put following:

        const images = {
            main_bg: require('./background.png'),
            main_logo: require('./auth/home_title.png'),
            ///you can add more many images like this here.
        };
    
    
    module.exports = images;
    

    When using the images, you can do like this:

    import Images from '../../assets/index'; ...

    0 讨论(0)
  • 2020-12-25 13:00

    To add a static image to your app, place it somewhere in your source code tree and reference it like this:

    <Image source={require('./my-icon.png')} />
    

    please see the below link for more explanation:

    https://facebook.github.io/react-native/docs/images.html

    0 讨论(0)
  • 2020-12-25 13:01

    You can add image folder to src(src/image/index.js). Image folder add index.js file create and add whole app image. In index.js file set

    export const IMAGENAME = require('./icon.png'); 
    

    When import image folder

    import { IMAGENAME } from '../image';
    

    Use image:

    <Image source={ IMAGENAME } />
    

    You can add image to image folder and set path to index file. Hope this will help you.

    0 讨论(0)
提交回复
热议问题