简体   繁体   English

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

[英]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.

相关问题 为什么我在React组件渲染中收到此警告“不赞成使用可变样式”? - Why am I get this warning ' Mutating `style` is deprecated' in react component render? “不推荐使用什么? 而是使用可组合性。”在Reactjs Material-UI menuItems中表示吗? - What does “Deprecated. Instead, use composability.” mean in Reactjs Material-UI menuItems? Heroku日志警告“(node)sys已弃用。使用util代替“脚本运行时 - Heroku log warning “(node) sys is deprecated. Use util instead” when script runs 创建弹出窗口会触发警告…$ tooltip现在已弃用。 使用$ uibTooltip代替 - Creating a popover triggers a warning… $tooltip is now deprecated. Use $uibTooltip instead Ant 设计表单“警告:不推荐使用`回调`。请改为返回承诺。” 自定义规则错误 - Ant design Form "Warning: `callback` is deprecated. Please return a promise instead." Error in custom rules Observable `of` 已弃用。 什么是等价物? - Observable `of` deprecated. What is the equivalent? Webpack 2:警告.png,.svg,..已弃用。 在它自己的选项中配置optipng的optimizationLevel选项。 (optipng.optimizationLevel) - Webpack 2: WARNING in .png, .svg, .. DEPRECATED. Configure optipng's optimizationLevel option in it's own options. (optipng.optimizationLevel) 已弃用“ webkitMovementX”。 请改用“ movementX” - 'webkitMovementX' is deprecated. Please use 'movementX' instead XMLHttpRequest 已弃用。 用什么代替? - XMLHttpRequest is deprecated. What to use instead? 不推荐使用getAttributeNodeNS()。 请改用getAttributeNS() - Use of getAttributeNodeNS() is deprecated. Use getAttributeNS() instead
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM