Skip to content

Commit d07283e

Browse files
authored
Use React.cloneElement instead of unfreezing
Instead of unfreezing and freezing the copied element, store the children and className results and use React.cloneElement to clone the element with the correct props.
1 parent f806b20 commit d07283e

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

src/linkClass.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,15 @@ const linkElement = (element: ReactElement, styles: Object, configuration: Objec
1414

1515
elementShallowCopy = element;
1616

17-
if (Object.isFrozen && Object.isFrozen(elementShallowCopy)) {
18-
elementIsFrozen = true;
19-
20-
// https://github.com/facebook/react/blob/v0.13.3/src/classic/element/ReactElement.js#L131
21-
elementShallowCopy = objectUnfreeze(elementShallowCopy);
22-
elementShallowCopy.props = objectUnfreeze(elementShallowCopy.props);
23-
}
17+
let children = elementShallowCopy.props.children;
18+
let className = elementShallowCopy.props.className;
2419

2520
const styleNames = parseStyleName(elementShallowCopy.props.styleName || '', configuration.allowMultiple);
2621

2722
if (React.isValidElement(elementShallowCopy.props.children)) {
28-
elementShallowCopy.props.children = linkElement(React.Children.only(elementShallowCopy.props.children), styles, configuration);
29-
} else if (_.isArray(elementShallowCopy.props.children) || isIterable(elementShallowCopy.props.children)) {
30-
elementShallowCopy.props.children = React.Children.map(elementShallowCopy.props.children, (node) => {
23+
children = linkElement(React.Children.only(children), styles, configuration);
24+
} else if (_.isArray(children) || isIterable(children)) {
25+
children = React.Children.map(children, (node) => {
3126
if (React.isValidElement(node)) {
3227
return linkElement(node, styles, configuration);
3328
} else {
@@ -40,20 +35,15 @@ const linkElement = (element: ReactElement, styles: Object, configuration: Objec
4035
appendClassName = generateAppendClassName(styles, styleNames, configuration.errorWhenNotFound);
4136

4237
if (appendClassName) {
43-
if (elementShallowCopy.props.className) {
44-
appendClassName = elementShallowCopy.props.className + ' ' + appendClassName;
38+
if (className) {
39+
appendClassName = className + ' ' + appendClassName;
4540
}
4641

47-
elementShallowCopy.props.className = appendClassName;
42+
className = appendClassName;
4843
}
4944
}
5045

51-
delete elementShallowCopy.props.styleName;
52-
53-
if (elementIsFrozen) {
54-
Object.freeze(elementShallowCopy.props);
55-
Object.freeze(elementShallowCopy);
56-
}
46+
elementShallowCopy = React.cloneElement(elementShallowCopy, { children, className, styleName: undefined });
5747

5848
return elementShallowCopy;
5949
};

0 commit comments

Comments
 (0)