react img 被自动转成base64,无法根据当前路径来动态改变值的解决办法

项目需求,需要根据当前的图片的路径值的来(加或者减)动态改变其值: state定义如下:

 

this.state={
            basket01:0+require("../../img/egg/egg1_00.png"),
            basket02:0+require('../../img/egg/egg2_00.png'),
            basket03:0+require('../../img/egg/egg3_00.png'),
            basket04:0+require('../../img/egg/egg4_00.png'),
        }  

 

 重点就是前面的数值:

 

render 代码如下:

<img className="egg-pen" src={this.state.basket01.substring(1,this.state.basket01.length)} alt=""/>

  

计算操作的方法代码为:

add (index){
        let basket01Num = this.state.basket01.substring(0,1)
        let basket02Num = this.state.basket02.substring(0,1)
        let basket03Num = this.state.basket03.substring(0,1)
        let basket04Num = this.state.basket04.substring(0,1)
        let eggSum = this.state.eggSum
        this.setState({
            eggSum:--eggSum
        })
        if(index === 1){
            ++basket01Num
            this.setState({
                basket01:basket01Num+require(`../../img/egg/egg1_0${basket01Num}.png`)
            })
        }else if(index === 2){
            ++basket02Num
            this.setState({
                basket02:basket02Num+require(`../../img/egg/egg2_0${basket02Num}.png`)
            })
        }else if(index === 3){
            ++basket03Num
            this.setState({
                basket03:basket03Num+require(`../../img/egg/egg3_0${basket03Num}.png`)
            })
        }else{
            ++basket04Num
            this.setState({
                basket04:basket04Num+require(`../../img/egg/egg4_0${basket04Num}.png`)
            })

        }
    }

  

项目实现的效果:

 

 通过加减来改变图片里面的鸡蛋数量

posted @ 2018-09-29 09:34  柠檬可乐  阅读(686)  评论(0编辑  收藏  举报