-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn if people mutate children. (#7001)
(cherry picked from commit 49238b9)
- Loading branch information
Showing
6 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/renderers/shared/devtools/ReactChildrenMutationWarningDevtool.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Copyright 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @providesModule ReactChildrenMutationWarningDevtool | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var ReactComponentTreeDevtool = require('ReactComponentTreeDevtool'); | ||
|
||
var warning = require('warning'); | ||
|
||
var elements = {}; | ||
|
||
function handleElement(debugID, element) { | ||
if (element == null) { | ||
return; | ||
} | ||
if (element._shadowChildren === undefined) { | ||
return; | ||
} | ||
if (element._shadowChildren === element.props.children) { | ||
return; | ||
} | ||
var isMutated = false; | ||
if (Array.isArray(element._shadowChildren)) { | ||
if (element._shadowChildren.length === element.props.children.length) { | ||
for (var i = 0; i < element._shadowChildren.length; i++) { | ||
if (element._shadowChildren[i] !== element.props.children[i]) { | ||
isMutated = true; | ||
} | ||
} | ||
} else { | ||
isMutated = true; | ||
} | ||
} | ||
warning( | ||
Array.isArray(element._shadowChildren) && !isMutated, | ||
'Component\'s children should not be mutated.%s', | ||
ReactComponentTreeDevtool.getStackAddendumByID(debugID), | ||
); | ||
} | ||
|
||
var ReactDOMUnknownPropertyDevtool = { | ||
onBeforeMountComponent(debugID, element) { | ||
elements[debugID] = element; | ||
}, | ||
onBeforeUpdateComponent(debugID, element) { | ||
elements[debugID] = element; | ||
}, | ||
onComponentHasMounted(debugID) { | ||
handleElement(debugID, elements[debugID]); | ||
delete elements[debugID]; | ||
}, | ||
onComponentHasUpdated(debugID) { | ||
handleElement(debugID, elements[debugID]); | ||
delete elements[debugID]; | ||
}, | ||
}; | ||
|
||
module.exports = ReactDOMUnknownPropertyDevtool; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters