ReactJs warning: Mutating `style` is deprecated. Consider cloning it beforehand

前端 未结 2 1250
被撕碎了的回忆
被撕碎了的回忆 2021-01-27 00:58

I am receving the following warning:

inWarning: `div` was passed a style object that has previously been mutated. Mutating `style` is deprecated. Consider clonin         


        
相关标签:
2条回答
  • 2021-01-27 01:37

    You are not cloning the previewColorHover

      var clone = Object.assign({}, this.styles.previewColorHover);
       this.styles.previewColorHover = clone;
       this.styles.previewColorHover.backgroundColor = this.state.selectedColor.hex
    
    0 讨论(0)
  • 2021-01-27 01:42

    You are cloning the selectedColor object but not the style object.

    do something as follows

    var clone = Object.assign({}, this.state.selectedColor);
    this.styles.previewColorHover.backgroundColor = clone.hex
    var style = {};
    style["previewColorHover"] = {backgroundColor : clone.hex}
    

    and use the style object in the div as

    <div ref='previewColor' id={'preview-color-' + this.props.id}
        style={style.previewColorHover}>
    </div>
    
    0 讨论(0)
提交回复
热议问题