gpt4 book ai didi

javascript - ReactJs 警告 : Mutating `style` is deprecated. 考虑事先克隆它

转载 作者:行者123 更新时间:2023-11-30 11:55:15 26 4
gpt4 key购买 nike

我收到以下警告:

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

关于javascript - ReactJs 警告 : Mutating `style` is deprecated. 考虑事先克隆它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38199056/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com