Skip to content

Commit

Permalink
Get rid of the previousStyleCopy
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sebmarkbage committed Nov 18, 2016
1 parent 151094c commit 447e0a1
Showing 1 changed file with 6 additions and 51 deletions.
57 changes: 6 additions & 51 deletions src/renderers/dom/fiber/ReactDOMFiberComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,43 +99,6 @@ function friendlyStringify(obj) {
return String(obj);
}

var styleMutationWarning = {};

function checkAndWarnForMutatedStyle(style1, style2, component) {
if (style1 == null || style2 == null) {
return;
}
if (shallowEqual(style1, style2)) {
return;
}

var componentName = component._tag;
var owner = component._currentElement._owner;
var ownerName;
if (owner) {
ownerName = owner.getName();
}

var hash = ownerName + '|' + componentName;

if (styleMutationWarning.hasOwnProperty(hash)) {
return;
}

styleMutationWarning[hash] = true;

warning(
false,
'`%s` was passed a style object that has previously been mutated. ' +
'Mutating `style` is deprecated. Consider cloning it beforehand. Check ' +
'the `render` %s. Previous style: %s. Mutated style: %s.',
componentName,
owner ? 'of `' + ownerName + '`' : 'using <' + componentName + '>',
friendlyStringify(style1),
friendlyStringify(style2)
);
}

/**
* @param {object} component
* @param {?object} props
Expand Down Expand Up @@ -526,14 +489,13 @@ function updateDOMProperties(
continue;
}
if (propKey === STYLE) {
var lastStyle = workInProgress._previousStyleCopy;
var lastStyle = lastProps[propKey];
for (styleName in lastStyle) {
if (lastStyle.hasOwnProperty(styleName)) {
styleUpdates = styleUpdates || {};
styleUpdates[styleName] = '';
}
}
workInProgress._previousStyleCopy = null;
} else if (registrationNameModules.hasOwnProperty(propKey)) {
// Do nothing for deleted listeners.
} else if (isCustomComponent(workInProgress._tag, lastProps)) {
Expand All @@ -552,26 +514,19 @@ function updateDOMProperties(
for (propKey in nextProps) {
var nextProp = nextProps[propKey];
var lastProp =
propKey === STYLE ? workInProgress._previousStyleCopy :
lastProps != null ? lastProps[propKey] : undefined;
if (!nextProps.hasOwnProperty(propKey) ||
nextProp === lastProp ||
nextProp == null && lastProp == null) {
continue;
}
if (propKey === STYLE) {
if (nextProp) {
if (__DEV__) {
checkAndWarnForMutatedStyle(
workInProgress._previousStyleCopy,
workInProgress._previousStyle,
workInProgress
);
workInProgress._previousStyle = nextProp;
if (__DEV__) {
if (nextProp) {
// Freeze the next style object so that we can assume it won't be
// mutated. We have already warned for this in the past.
Object.freeze(nextProp);
}
nextProp = workInProgress._previousStyleCopy = Object.assign({}, nextProp);
} else {
workInProgress._previousStyleCopy = null;
}
if (lastProp) {
// Unset styles on `lastProp` but not on `nextProp`.
Expand Down

0 comments on commit 447e0a1

Please sign in to comment.