← Back to React Course | Chapter 11: Styling in React | Lesson 2 of 8

Styled Components का उपयोग

styled-components ऐसे हैं जैसे आप अपना CSS सीधे हर component पर चिपकी एक sticky note पर लिखें, styles को room के दूसरी तरफ एक अलग binder में रखने के बजाय।
Syntax
markup
const StyledName = styled.tag`
  property: value;
  color: ${props => (props.active ? "blue" : "gray")};

  &:hover {
    property: value;
  }
`;

<StyledName active>Content</StyledName>

JavaScript में सीधे CSS लिखना

styled-components आपको एक component की styling define करने देता है real CSS syntax के एक tagged template literal का उपयोग करके, जो सीधे एक styled.tagname call से attached है। Result, जैसे StyledButton, एक genuine React component है जिसे आप बिल्कुल किसी और की तरह render करते हैं।

Note: Styled components को एक clear prefix या suffix के साथ नाम दें, जैसे StyledButton, ताकि उन्हें plain components से एक नज़र में अलग किया जा सके।
Warning: यह एक npm package है — special styled.div`` syntax को library के Babel/runtime processing की ज़रूरत है और यह इस CDN-only sandbox में standalone काम नहीं करेगा।

उदाहरण: Writing CSS Directly in JavaScript

markup
// Run in your local React project (npm install required)
import styled from 'styled-components';

const StyledButton = styled.button`
  background: blue;
  color: white;
  padding: 8px 16px;
`;

function App() {
  return <StyledButton>Click me</StyledButton>;
}

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Dynamic Styling के लिए Props उपयोग करना

Template literal के अंदर, आप एक function embed कर सकते हैं जो component के props receive करता है और एक CSS value return करता है, जिससे वही styled component अलग props दिए जाने पर अलग render हो सके, बिना अलग CSS classes लिखे।

Note: Template literal के अंदर prop-based logic को simple रखें — complex conditional styling के लिए, पहले से एक value compute करके उसे ही pass करने पर विचार करें।
Warning: एक styled component को pass किया गया primary जैसा prop default रूप से older versions में underlying DOM element को भी forward हो जाता है, जो React 'unknown prop' warnings का कारण बन सकता है — अपने styled-components version का behavior check करें।

उदाहरण: Using Props for Dynamic Styling

markup
// Run in your local React project (npm install required)
import styled from 'styled-components';

const StyledButton = styled.button`
  background: ${props => props.primary ? 'blue' : 'gray'};
  color: white;
`;

function App() {
  return <StyledButton primary>Primary Button</StyledButton>;
}

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Nesting और Pseudo-Selectors

Inline styles के विपरीत, styled-components के template literals real CSS की FULL power support करते हैं, nested selectors और pseudo-classes जैसे &:hover सहित, '&' symbol उपयोग करते हुए component को खुद refer करने के लिए।

Note: यह nesting support styled-components के सबसे बड़े advantages में से एक है plain inline React styles के ऊपर, जो hover/focus states express नहीं कर सकतीं।
Warning: '&' को component के अपने root element को refer करना चाहिए — अनरिलेटेड selectors को deeply nest करना styles को उनके component से trace back करना मुश्किल बना सकता है।

उदाहरण: Nesting and Pseudo-Selectors

markup
// Run in your local React project (npm install required)
import styled from 'styled-components';

const StyledButton = styled.button`
  background: blue;
  &:hover {
    background: darkblue;
  }
`;

function App() {
  return <StyledButton>Hover me</StyledButton>;
}

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक styled component को दूसरे component के render function के अंदर define करना, जो हर render पर इसे recreate करता है (और इसकी कोई भी internal state खो देता है)।
  2. यह भूल जाना कि styled.div``... एक असली React component बनाता है, किसी और की तरह usable, अपने खुद के props के साथ।
  3. यह न realize करना कि template-literal CSS full nesting और pseudo-selectors (जैसे &:hover) support करता है, सिर्फ flat properties नहीं।
चैप्टर सारांश
  • styled-components आपको JavaScript template literals के अंदर असली CSS लिखने देता है, एक specific component से attached।
  • styled.div... (या styled.button, styled.p, आदि) एक नया React component return करता है जिसमें वे styles baked in हैं।
  • Props को template literal के अंदर उपयोग करके styles conditional/dynamic बनाई जा सकती हैं।
  • Styled components को module scope पर define किया जाना चाहिए, किसी और component के render के अंदर recreate किए जाने के बजाय।
ब्राउज़र सपोर्ट

npm install styled-components चाहिए — इस sandbox में CDN के ज़रिए available नहीं; React 16.8+ के साथ काम करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.