为什么不推荐使用变异风格? [英] Why was mutating style deprecated?

查看:41
本文介绍了为什么不推荐使用变异风格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

0.13 和 0.14 中的文档都警告说变异样式已被弃用,但没有提及原因

The documentation in 0.13 and 0.14 both warn that mutating style has been deprecated, but don't mention why

在渲染之间重用和改变样式对象已被弃用

Reusing and mutating a style object between renders has been deprecated

如果我想对基于 css 类的动画无法处理的元素执行状态相关的动画,我该怎么办?每次都克隆对象?

What should I do if I want to perform state-dependent animations on an element that css class-based animations can't handle? Clone the object each time?

非常感谢新人的反应、帮助和建议

New to react, help and advice much appreciated

推荐答案

查看测试用例/预期错误,您是正确的,应该克隆对象.

Looking at the test case/expected error, you are correct and should be cloning the object.

Warning: `div` was passed a style object that has previously been
mutated. Mutating `style` is deprecated. Consider cloning it
beforehand. Check the `render` of `App`. Previous style:
{border: "1px solid black"}. Mutated style:
{border: "1px solid black", position: "absolute"}.

tests/ReactDOMComponent-test.js#L128

https://github.com/facebook/react/blob/fc96f31fade8043856eb7bc34c56598c4a576110/src/renderers/dom/shared/tests/ReactDOMComponent-test.js#L128

我猜这类似于 props 的推理 - 它使您免于到处传递可变样式对象,并最终失去了 React 非常擅长帮助您进行推理的很多简单性.

I would guess it's similar to the reasoning for props - it saves you from passing a mutable style object around everywhere and ending up losing a lot of the ease of reasoning that React is really good at helping you with.

props 对象现在已冻结,因此不再支持在创建组件元素后更改 props.在大多数情况下,应该使用 React.cloneElement 代替.此更改使您的组件更易于推理并启用上述编译器优化.

这篇关于为什么不推荐使用变异风格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆