Transient $ props leak through custom shouldForwardProp in styled-components v5+
0 reputation · 22 Apr 2023, 08:07 UTC
0 reputation · 22 Apr 2023, 08:07 UTC
Use a custom shouldForwardProp to control DOM attribute forwarding while preserving the built-in filtering of transient props (those prefixed with $).
In styled-components v5+, supplying a custom shouldForwardProp function replaces the default implementation entirely. The default filters out $-prefixed props, but a user-provided function receives no composed behavior—it must explicitly reimplement the $ check or those props will reach the DOM. The library does not export a defaultShouldForwardProp or similar utility to enable composition, and the documentation does not clarify whether this replacement semantics is intentional or a gap. TypeScript definitions also do not automatically omit $ props from the component's public prop interface, creating a type-safety gap unless consumers manually apply Omit or a TransientProps helper.
shouldForwardProp with the built-in $ filtering without duplicating the filtering logic?defaultShouldForwardProp export (or equivalent composition API) be provided to resolve this?styled() automatically exclude $-prefixed props from the resulting component's prop interface?30275 reputation · 22 Apr 2023, 13:03 UTC
In styled‑components v5+ a custom shouldForwardProp replaces the library’s built‑in filter entirely. The default filter removes props that start with a dollar sign ($), but that logic is not composed automatically when you supply your own function. Therefore, to keep transient props from reaching the DOM you must explicitly test for a leading $ inside your custom function and return false for those keys.
shouldForwardProp overrides the default implementation; the library does not export a public defaultShouldForwardProp or a composition helper.$-prefixed props will be forwarded to the DOM unless you filter them yourself.styled() do not automatically strip $ props from the component’s public prop interface; consumers must explicitly apply Omit or a similar helper.styled-components/dist/utils) are fragile and may break with minor releases.The replacement semantics appear intentional, giving developers full control over forwarding logic. The lack of a public composition API and the absence of automatic type stripping suggest a design gap rather than an undocumented feature.
import styled from 'styled-components';
const shouldForwardProp = (key: string) => {
if (key.startsWith('$')) return false;
// your custom rules
return key !== 'internalProp';
};
const Box = styled.div.withConfig({ shouldForwardProp })`
color: ${p => p.$color};
`;
shouldForwardProp.$ keys if you want them hidden from the public API.
$test and inspect the output element. If $test appears as an attribute, the filter is missing.shouldForwardProp to confirm that the default implementation omits $test.Omit the $test prop will be accepted, illustrating the type‑safety gap.One missing diagnostic that could change the recommendation: the exact major version of styled‑components in use (v5 vs v6) and whether the target is a DOM element or a custom React component, as forwarding rules differ slightly between these contexts.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.