你能在 React native 中创建动态图像转换器吗?

Can you create a dynamic image changer in React native?

你能在 React native 中创建动态图像转换器吗?

const btnRender =(txtButton, ImgUrl)=>{
    let image = '../../assets/icons/objects/dog.png'
    ImgUrl ? image = ImgUrl : '../../assets/icons/objects/dog.png'

    return(
        <TouchableOpacity style={styles.picContainer}>
            <Image source={require(image)} style={styles.icons}></Image>
        </TouchableOpacity>
    )
}

{btnRender("test1", '../../assets/icons/objects/cat.png')}

你不能动态要求一个文件,你需要先在内存上分配,然后再分配它:

const btnRender =(txtButton, ImgUrl)=>{
    var image = require('../../assets/icons/objects/dog.png');

    if(ImgUrl !== undefined){
      image = ImgUrl;
    }

    return(
        <TouchableOpacity style={styles.picContainer}>
            <Image source={image} style={styles.icons}></Image>
        </TouchableOpacity>
    )
  } 

  btnRender("test1",require("../ThePathOfTheImage"))