[英]ReactJs warning: Mutating `style` is deprecated. Consider cloning it beforehand
I am receving the following warning: 我收到以下警告:
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"}.
When trying to assign a style property to a div even after cloning the original object (I have also tried using JSON.parse(JSON.stringify()) with no sucess). 在尝试将一个样式属性分配给div时,即使克隆了原始对象(我也尝试过使用JSON.parse(JSON.stringify())而没有成功)。
Could you tell me why I am receiving this error and how to fix it. 你能告诉我为什么我收到这个错误以及如何解决它。
var clone = Object.assign({}, this.state.selectedColor);
this.styles.previewColorHover.backgroundColor = clone.hex
in my render function: 在我的渲染功能中:
<div ref='previewColor' id={'preview-color-' + this.props.id}
style={this.styles.previewColorHover}>
</div>
You are not cloning the previewColorHover 您没有克隆previewColorHover
var clone = Object.assign({}, this.styles.previewColorHover);
this.styles.previewColorHover = clone;
this.styles.previewColorHover.backgroundColor = this.state.selectedColor.hex
You are cloning the selectedColor object but not the style object. 您正在克隆selectedColor对象而不是样式对象。
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中的样式对象作为
<div ref='previewColor' id={'preview-color-' + this.props.id}
style={style.previewColorHover}>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.