小编典典

为什么不赞成使用变异样式?

reactjs

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

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

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

新的反应,帮助和建议,不胜感激


阅读 207

收藏
2020-07-22

共1个答案

小编典典

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

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"}.

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

我想这与道具的推理类似-它使您免于在任何地方传递可变样式对象,并最终失去了React真正擅长为您提供帮助的大量推理功能。

The props object is now frozen, so mutating props after creating a component element is no longer supported. In most cases, React.cloneElement should be used instead. This change makes your components easier to reason about and enables the compiler optimizations mentioned above.

2020-07-22