Get rid of the previousStyleCopy
We have warned about not mutating styles already. At this point we can just freeze the style object in DEV. That way we can read it from the previous props object without storing another copy of it. Also delete the associated warning.
Sebastian Markbage committed
Nov 15, 2016 at 18:01 UTC
447e0a12b55f4570f62872e6ce50439704416eef
1 file changed
+6
-51
src/renderers/dom/fiber/ReactDOMFiberComponent.js
+6
-51
@@ -99,43 +99,6 @@ function friendlyStringify(obj) {
99
return String(obj);
100
}
101
102
-var styleMutationWarning = {};
103
-
104
-function checkAndWarnForMutatedStyle(style1, style2, component) {
105
- if (style1 == null || style2 == null) {
106
- return;
107
- }
108
- if (shallowEqual(style1, style2)) {
109
- return;
110
- }
111
-
112
- var componentName = component._tag;
113
- var owner = component._currentElement._owner;
114
- var ownerName;
115
- if (owner) {
116
- ownerName = owner.getName();
117
- }
118
-
119
- var hash = ownerName + '|' + componentName;
120
-
121
- if (styleMutationWarning.hasOwnProperty(hash)) {
122
- return;
123
- }
124
-
125
- styleMutationWarning[hash] = true;
126
-
127
- warning(
128
- false,
129
- '`%s` was passed a style object that has previously been mutated. ' +
130
- 'Mutating `style` is deprecated. Consider cloning it beforehand. Check ' +
131
- 'the `render` %s. Previous style: %s. Mutated style: %s.',
132
- componentName,
133
- owner ? 'of `' + ownerName + '`' : 'using <' + componentName + '>',
134
- friendlyStringify(style1),
135
- friendlyStringify(style2)
136
- );
137
-}
138
-
102
/**
103
* @param {object} component
104
* @param {?object} props
@@ -526,14 +489,13 @@ function updateDOMProperties(
489
continue;
490
}
491
if (propKey === STYLE) {
529
- var lastStyle = workInProgress._previousStyleCopy;
492
+ var lastStyle = lastProps[propKey];
493
for (styleName in lastStyle) {
494
if (lastStyle.hasOwnProperty(styleName)) {
495
styleUpdates = styleUpdates || {};
496
styleUpdates[styleName] = '';
497
}
498
}
536
- workInProgress._previousStyleCopy = null;
499
} else if (registrationNameModules.hasOwnProperty(propKey)) {
500
// Do nothing for deleted listeners.
501
} else if (isCustomComponent(workInProgress._tag, lastProps)) {
@@ -552,7 +514,6 @@ function updateDOMProperties(
514
for (propKey in nextProps) {
515
var nextProp = nextProps[propKey];
516
var lastProp =
555
- propKey === STYLE ? workInProgress._previousStyleCopy :
517
lastProps != null ? lastProps[propKey] : undefined;
518
if (!nextProps.hasOwnProperty(propKey) ||
519
nextProp === lastProp ||
@@ -560,18 +521,12 @@ function updateDOMProperties(
521
continue;
522
}
523
if (propKey === STYLE) {
563
- if (nextProp) {
564
- if (__DEV__) {
565
- checkAndWarnForMutatedStyle(
566
- workInProgress._previousStyleCopy,
567
- workInProgress._previousStyle,
568
- workInProgress
569
- );
570
- workInProgress._previousStyle = nextProp;
524
+ if (__DEV__) {
525
+ if (nextProp) {
526
+ // Freeze the next style object so that we can assume it won't be
527
+ // mutated. We have already warned for this in the past.
528
+ Object.freeze(nextProp);
529
}
572
- nextProp = workInProgress._previousStyleCopy = Object.assign({}, nextProp);
573
- } else {
574
- workInProgress._previousStyleCopy = null;
530
}
531
if (lastProp) {
532
// Unset styles on `lastProp` but not on `nextProp`.