ReactJs 警告:不推荐使用变异`style`.考虑事先克隆它 [英] ReactJs warning: Mutating `style` is deprecated. Consider cloning it beforehand
本文介绍了ReactJs 警告:不推荐使用变异`style`.考虑事先克隆它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到以下警告:
inWarning: `div` 被传递了一个之前已经改变的样式对象.不推荐使用变异的 `style`.考虑事先克隆它.检查`xxx`的`render`.以前的样式:{backgroundColor: "#000000"}.突变样式:{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}>
解决方案
您不是在克隆 previewColorHover
var clone = Object.assign({}, this.styles.previewColorHover);this.styles.previewColorHover = 克隆;this.styles.previewColorHover.backgroundColor = this.state.selectedColor.hex
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).
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
var clone = Object.assign({}, this.styles.previewColorHover);
this.styles.previewColorHover = clone;
this.styles.previewColorHover.backgroundColor = this.state.selectedColor.hex
这篇关于ReactJs 警告:不推荐使用变异`style`.考虑事先克隆它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文