🔧 Programmierung 🕛 vor 2 Monaten 2 Min Lesezeit
0

What is a Transient Prop?

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

A transient prop is a special prop in styled-components that starts with a dollar sign ($).



Example:




CODE
<StyledRow $columns="1fr 2fr 1fr" />






Inside the styled component:




CODE
const StyledRow = styled.div`
grid-template-columns:
${(props) => props.$columns};
`
;






The $ tells styled-components:




"This prop is only for styling. Do not pass it to the HTML element."










Why do Transient Props exist?



Sometimes we pass values only to generate CSS.



For example:




  • Number of grid columns

  • Background color

  • Padding

  • Width

  • Border radius

  • Active state



These values are needed by styled-components, but the browser's HTML elements (div, button, section, etc.) do not understand them.



Without transient props, React forwards those props to the DOM, which causes warnings like:




CODE
Warning: Unknown prop "columns" on <div>.






Transient props solve this problem by keeping styling props inside styled-components and preventing them from reaching the DOM.









Normal Prop vs Transient Prop






Normal Prop






CODE
<StyledRow columns="1fr 2fr" />









CODE
const StyledRow = styled.div`
grid-template-columns:
${(props) => props.columns};
`
;






React renders:




CODE
<div columns="1fr 2fr"></div>






Since columns is not a valid HTML attribute, React shows a warning.









Transient Prop






CODE
<StyledRow $columns="1fr 2fr" />









CODE
const StyledRow = styled.div`
grid-template-columns:
${(props) => props.$columns};
`
;






React renders:




CODE
<div></div>






The $columns prop is used only by styled-components to generate CSS and is not passed to the DOM.









Key Differences




























Normal Prop Transient Prop
columns $columns
Passed to the DOM Not passed to the DOM
Can trigger React warnings Prevents React warnings
Used for component logic or data Used only for styling








When Should You Use Transient Props?



Use transient props whenever a prop is only needed for styling.



Examples:




CODE
<Button $primary />
<Card $active />
<Box $padding="2rem" />
<Grid $columns="1fr 2fr 1fr" />






If the prop is only used to create CSS and should not become an HTML attribute, make it a transient prop by adding the $ prefix.









Summary



A transient prop is a styled-components feature that uses the $ prefix to mark styling-only props. These props are available inside the styled component but are not forwarded to the browser's HTML elements. This keeps the DOM clean, avoids React warnings, and makes your components easier to maintain.

Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
9 Quellen
CVE-2022-44169 | Tenda AC15 15.03.05.18 formSetVirtualSer buffer overflow (EUVD-2022-47119)
1 Quelle
Best early October Prime Day deals: Save on TVs, smartwatches, and more tech
1 Quelle
I gave Claude Code $100 and 30 days to make a profit. Day 1, it built a product. Here's the pattern it used.
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten What is a Transient Prop?

Thematisch verwandte Begriffe: What, Transient, Prop · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...