← Back to React Course | Chapter 3: Components & Props | Lesson 2 of 11

Class Components (पुराना तरीका)

Class components एक React component लिखने का पुराना, लंबा तरीका हैं -- एक letter हाथ से लिखने जैसा बजाय इसे type करने के।
Syntax
markup
class ComponentName extends React.Component {
  render() {
    return <element>{this.props.propName}</element>;
  }
}

एक Class Component Define करना

एक class component ES6 class syntax उपयोग करके लिखा जाता है और इसे React.Component extend करना चाहिए। एक plain return statement के बजाय, यह एक render() method define करता है जो display होने वाला JSX return करता है।

Hooks मौजूद होने से पहले React में state use करने का यह इकलौता तरीका था।

Note: आप class components को आमतौर पर पुराने React codebases पढ़ते समय encounter करेंगे, नया code लिखते समय नहीं।
Warning: React.Component extend करना भूल जाने का मतलब है React आपकी class को एक valid component के रूप में recognize नहीं करेगा।

उदाहरण: Defining a Class Component

markup
<!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">
    class Welcome extends React.Component {
      render() {
        return <h1>Welcome (class component)!</h1>;
      }
    }
    ReactDOM.createRoot(document.getElementById('root')).render(<Welcome />);
  </script>
</body>
</html>

Class Component में Props Access करना

एक class component के अंदर, props एक function argument के बजाय this.props के through access किए जाते हैं। इसके लिए constructor में super(props) call करना ज़रूरी है अगर आप एक define करते हैं, ताकि React आपके लिए this.props properly wire up कर सके।

Note: अगर आपको constructor की ज़रूरत नहीं, आप इसे पूरी तरह skip कर सकते हैं -- this.props render() में फिर भी ठीक काम करता है।
Warning: पहले super(props) call किए बिना एक constructor define करना एक runtime error का कारण बनता है।

उदाहरण: Accessing Props in a Class Component

markup
<!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">
    class Greeting extends React.Component {
      render() {
        return <p>Hello, {this.props.name}!</p>;
      }
    }
    ReactDOM.createRoot(document.getElementById('root')).render(<Greeting name="Sam" />);
  </script>
</body>
</html>

Class vs Function Components

Hooks के साथ Function components अब वह सब कर सकते हैं जो class components कर सकते हैं, कम code और this की ज़रूरत के बिना उपयोग करते हुए। इसी कारण, modern React documentation और ज़्यादातर नए projects function components ही exclusively use करते हैं, class components को सिर्फ legacy code maintenance के लिए रखते हुए।

Note: अगर आप आज React सीख रहे हैं, function components पर focus करें -- आपको शायद ही कभी एक नया class component लिखना पड़ेगा।
Warning: this.state/this.setState (class syntax) को useState (Hook syntax) के साथ mix up करना पुराने tutorials पढ़ते समय confusion का एक आम source है।

उदाहरण: Class vs Function Components

markup
<!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 ModernWay() {
      return <p>Function component: simpler syntax</p>;
    }
    class OldWay extends React.Component {
      render() {
        return <p>Class component: more boilerplate</p>;
      }
    }
    function App() {
      return <div><ModernWay /><OldWay /></div>;
    }
    ReactDOM.createRoot(document.getElementById('root')).render(<App />);
  </script>
</body>
</html>
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. Constructor के अंदर super(props) call करना भूल जाना, जो this.props तक access तोड़ता है।
  2. Class component के अंदर Hooks (जैसे useState) use करने की कोशिश करना -- Hooks सिर्फ function components में काम करते हैं।
  3. यह भूल जाना कि render() method को JSX return करना चाहिए, बिल्कुल एक function component के return value जैसा।
चैप्टर सारांश
  • Class components ES6 classes उपयोग करके React components लिखने का एक पुराना syntax है।
  • उन्हें React.Component extend करना और एक render() method define करना चाहिए।
  • State और lifecycle methods function components में use होने वाले Hooks से अलग तरीके से काम करते हैं।
  • Modern React code Hooks के साथ function components को favor करता है; class components legacy माने जाते हैं।
ब्राउज़र सपोर्ट

Class components React के earliest versions से supported रहे हैं और React 18 में अब भी supported हैं, हालांकि वे अब recommended approach नहीं हैं।

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.