JSX Attributes और className
In this page:
<element className="class-name" attributeName={expression} />
<element style={{ camelCaseProperty: "value" }} />
class के बजाय className Set करना
क्योंकि class JavaScript में एक reserved word है, JSX एक element पर CSS class apply करने के लिए HTML के class attribute के बजाय className उपयोग करता है।
उदाहरण: Setting className Instead of class
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function StyledBox() {
return (
<div className="box" style={{border: '2px solid #333', padding: '8px'}}>
Styled with className
</div>
);
}
ReactDOM.createRoot(document.getElementById('root')).render(<StyledBox />);
</script>
</body>
</html>
Dynamic Attribute Values Pass करना
कोई भी JSX attribute curly braces उपयोग करके एक dynamic value ले सकता है, जैसे src={imageUrl} या disabled={isLoading}, एक fixed string के बजाय।
उदाहरण: Passing Dynamic Attribute Values
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function DynamicButton() {
const isLoading = true;
return <button disabled={isLoading}>{isLoading ? 'Loading...' : 'Submit'}</button>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<DynamicButton />);
</script>
</body>
</html>
style Attribute एक Object लेता है
HTML के विपरीत, JSX का style attribute एक JavaScript object लेता है जिसमें camelCased CSS properties होती हैं, एक semicolon-separated string नहीं।
उदाहरण: The style Attribute Takes an Object
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function InlineStyled() {
return <p style={{ color: 'blue', fontWeight: 'bold' }}>Styled inline</p>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<InlineStyled />);
</script>
</body>
</html>
classNameके बजायclassउपयोग करना, जो JSX में silently कुछ नहीं करता।- Dynamic attribute values को quotes के बजाय curly braces में wrap करना भूल जाना।
- label elements पर
htmlForके बजायforउपयोग करना।
- JSX attributes camelCase names उपयोग करते हैं, lowercase HTML attributes के विपरीत।
classNameclassकी जगह लेता है;htmlForforकी जगह लेता है।- Static attribute values quotes उपयोग करती हैं; dynamic values curly braces उपयोग करती हैं।
onClickजैसे event handler attributes एक function reference लेते हैं, string नहीं।
कोई browser-specific restrictions नहीं -- attribute naming एक JSX convention है, कोई runtime feature नहीं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: