Skip to content

Commit

Permalink
Merge pull request #3964 from jportner/frozen-prototype-fix-v5
Browse files Browse the repository at this point in the history
[v5] Make styled components work when the Object prototype is frozen
  • Loading branch information
quantizor committed Mar 13, 2023
2 parents d68365d + 3a9f8be commit e7602e6
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/styled-components/src/models/StyledComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,11 @@ export default function createStyledComponent(
);
}

WrappedStyledComponent.toString = () => `.${WrappedStyledComponent.styledComponentId}`;
// If the Object prototype is frozen, the "toString" property is non-writable. This means that any objects which inherit this property
// cannot have the property changed using an assignment. If using strict mode, attempting that will cause an error. If not using strict
// mode, attempting that will be silently ignored.
// However, we can still explicitly shadow the prototype's "toString" property by defining a new "toString" property on this object.
Object.defineProperty(WrappedStyledComponent, 'toString', { value: () => `.${WrappedStyledComponent.styledComponentId}` });

if (isCompositeComponent) {
hoist<
Expand Down

0 comments on commit e7602e6

Please sign in to comment.