繁体   English   中英

ReactJs警告:不推荐使用变体`style`。 考虑事先克隆它

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

我收到以下警告:

inWarning: `div` was passed a style object that has previously been mutated. Mutating `style` is deprecated. Consider cloning it beforehand. Check the `render` of `xxx`. Previous style: {backgroundColor: "#000000"}. Mutated style: {backgroundColor: "#002a09"}. 

在尝试将一个样式属性分配给div时,即使克隆了原始对象(我也尝试过使用JSON.parse(JSON.stringify())而没有成功)。

你能告诉我为什么我收到这个错误以及如何解决它。

   var clone = Object.assign({}, this.state.selectedColor);
   this.styles.previewColorHover.backgroundColor = clone.hex

在我的渲染功能中:

<div ref='previewColor' id={'preview-color-' + this.props.id}
    style={this.styles.previewColorHover}>
</div>

您没有克隆previewColorHover

  var clone = Object.assign({}, this.styles.previewColorHover);
   this.styles.previewColorHover = clone;
   this.styles.previewColorHover.backgroundColor = this.state.selectedColor.hex

您正在克隆selectedColor对象而不是样式对象。

做一些如下事情

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

并使用div中的样式对象作为

<div ref='previewColor' id={'preview-color-' + this.props.id}
    style={style.previewColorHover}>
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM