Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v5] Make styled components work when the Object prototype is frozen #3964

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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