How can I conditionally include images in React Native Component?

后端 未结 1 1432
清歌不尽
清歌不尽 2021-01-22 04:30

I am able to add static images to a ListView cell just fine (see code below), but how do I change the icon image dynamically?

From React Native Docs

相关标签:
1条回答
  • 2021-01-22 04:55

    The images have to be known during packaging. There's a section about it in the docs.

    Put this at the top of the file you define ExpandingCell in:

    const MAGNIFYING_GLASS_ICON = require('./CellIcons/MagnifyingGlassIcon.png');
    

    Then you can use the constant like this

    let icon = someCondition ? MAGNIFYING_GLASS_ICON : SOME_OTHER_ICON;
    <Image source={icon}/>
    

    You have to have the requires for all images you want to use this way in there.

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