<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vahagn Ghaldunts on Medium]]></title>
        <description><![CDATA[Stories by Vahagn Ghaldunts on Medium]]></description>
        <link>https://medium.com/@vahagnghaldunts07?source=rss-cc23f8fe25d4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ZaIIZLLo6zkuPJCSo8ZeLQ.jpeg</url>
            <title>Stories by Vahagn Ghaldunts on Medium</title>
            <link>https://medium.com/@vahagnghaldunts07?source=rss-cc23f8fe25d4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 11:35:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vahagnghaldunts07/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[React Component’s Lifecycle Methods]]></title>
            <link>https://medium.com/@vahagnghaldunts07/react-components-lifecycle-methods-6e3b503c5490?source=rss-cc23f8fe25d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e3b503c5490</guid>
            <category><![CDATA[react-component-library]]></category>
            <category><![CDATA[react-lifecycle]]></category>
            <category><![CDATA[react-component-lifecycle]]></category>
            <category><![CDATA[react-lifecycle-method]]></category>
            <category><![CDATA[react-hook]]></category>
            <dc:creator><![CDATA[Vahagn Ghaldunts]]></dc:creator>
            <pubDate>Tue, 07 May 2024 14:41:52 GMT</pubDate>
            <atom:updated>2024-05-28T13:35:23.974Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XH_XYhAOOrGYHL7mmfIAAw.jpeg" /><figcaption><strong>React Component’s Lifecycle</strong></figcaption></figure><p>React-ում բաղադրիչների կյանքի ցիկլը(component’s Lifecylce) բաղկացած է տարբեր փուլերից: Յուրաքանչյուր փուլ ունի lifecycle-ի մի շարք մեթոդներ, որոնք կանչվում են component-ի lifecycle-ի որոշակի կետերում: Այս մեթոդները թույլ են տալիս որոշակի գործողություններ կատարել lifecycle-ի տարբեր փուլերում, ինչպիսիք են` վերահսկել բաղադրիչի վարքագիծը(component’s behavior), API-ի հարցումների կատարումը, UI(User Interface)-ի թարմացումը կամ ռեսուրսների մաքրումը:</p><p>Component’s Lifecycle-ը բաղկացած է երեք հիմնական փուլերից ՝ մոնտաժման փուլ(<strong>Mounting</strong>), թարմացման փուլ(<strong>Updating</strong>)և ապամոնտաժման փուլ(<strong>Unmounting</strong>):</p><p><strong>Mounting</strong> <strong>Phase</strong>-ը սկսվում է այն ժամանակ, երբ component-ն առաջին անգամ ստեղծվում և տեղադրվում է DOM (Document Object Model)-ում: <br><strong>Updating</strong> <strong>Phase</strong>-ը տեղի է ունենում այն ժամանակ, երբ component-ի state-ը կամ props-ը փոխվում է: <br><strong>Unmounting Phase-</strong>ը տեղի է ունենում այն ժամանակ, երբ component-ը հեռացվում է DOM-ից:</p><h3>Component Mounting Phase</h3><p><strong>Mounting </strong>փուլը վերաբերում է այն ժամանակահատվածին, երբ component-ը ստեղծվում և տեղադրվում է DOM-ում:</p><p>Այս փուլի ընթացքում React-ի կողմից գործարկվում են lifecycle-ի մի քանի մեթոդներ, որոնք հնարավորւթյուն են տալիս ծրագրավորողին կարգավորել component-ը, ստեղծել ցանկացած անհրաժեշտ վիճակ(state) , և կատարել այլ սկզբնավորման առաջադրանքներ(initialization tasks):</p><blockquote><strong>Mounting</strong> փուլի կյանքի ցիկլի մեթոդները լինում են`<br>1) The <strong><em>constructor()</em></strong><em> </em>lifecycle method<br>2) The <strong><em>render()</em></strong> lifecycle method<br>3) The <strong><em>getDerivedStateFromProps()</em></strong> lifecycle method<br>4) The <strong><em>componentDidMount()</em></strong> lifecycle method</blockquote><blockquote>The constructor( ) lifecycle method</blockquote><p><strong>constructor( ) </strong>մեթոդը կանչվում է, երբ component-ն առաջին անգամ ստեղծվում է: Այն կարող ենք սահմանել component-ի state-ը և արժեքները(value)` արժեքավորելու(initialize) համար: constructor( )-ը ստանում է component-ի <strong>props</strong>-ը որպես արգումենտ և միշտ պետք է կանչվի <strong>super(props)</strong>ցանկացած այլ հայտարարությունից առաջ:<br> Օրինակ.</p><pre>class MyComponent extends React.Component {<br>  constructor(props) {<br>    super(props);<br>    this.state = {<br>      count: 0<br>    };<br>  }<br>}</pre><blockquote>The render( ) lifecycle method</blockquote><p><strong>render( )</strong> մեթոդը lifecycle-ի ամենակարևոր մեթոդն է և պահանջվում է յուրաքանչյուր component-ում: Այն պատասխանատու է component-ի virtual DOM-ի ներկայացման, գեներացման համար` հիմնվելով դրա ընթացիկ props-ի և state-ի վրա: Այն կանչվում է ամեն անգամ, երբ component-ը պետք է վերարտադրվի(re-rendered), կա՛մ այն ​​պատճառով, որ դրա props-ը կամ state-ը փոխվել է, կա՛մ էլ այն ​​պատճառով, որ մայր բաղադրիչը(parent component) վերաարտադրվել է(re-rendered):</p><p>render( ) մեթոդը պետք է լինի մաքուր, այսինքն` այն չպետք է փոփոխի component-ի state-ը կամ ուղղակիորեն փոխազդի բրաուզերի հետ:<br>Օրինակ.</p><pre>class MyComponent extends React.Component {<br>  render() {<br>    return &lt;div&gt;{this.props.message}&lt;/div&gt;;<br>  }<br>}</pre><blockquote>The getDerivedStateFromProps( ) lifecycle method</blockquote><p><strong>getDerivedStateFromProps( )</strong> մեթոդը հասանելի է React 16.3-ից սկսած տարբերակներում, կանչվում է component-ի Mounting և Updating փուլերում:<br>Mounting փուլում <strong>getDerivedStateFromProps()</strong> կանչվում է constructor-ից հետո և մինչև <strong>render()</strong>: Այս մեթոդը կանչված է յուրաքանչյուր արտապատկերման ցիկլի(render cycle) համար և հնարավորւթյուն է տալիս թարմացնել component-ի state-ը` հիմնվելով props-ի փոփոխությունների վրա: <br>Այս մեթոդը վերադարձնում է օբյեկտ, որը ներկայացնում է component-ի թարմացված state-ը կամ <strong>null ` </strong>եթե state-ի թարմացում չկա:</p><p>Կարևոր է նշել, որ <strong>getDerivedStateFromProps()</strong>-ը ստատիկ մեթոդ է, ինչը նշանակում է, որ այն մուտք չունի <strong>this</strong> keyword-ին և չի կարող փոխազդել component-ի օրինակների մեթոդների կամ property-ների հետ:</p><pre>class MyComponent extends React.Component {<br>  static getDerivedStateFromProps(props, state) {<br>    // Update the state based on changes in props<br>    // Return an object representing the updated state or null<br>  }<br>}</pre><blockquote>The componentDidMount( ) lifecycle method</blockquote><p><strong>componentDidMount( ) </strong>մեթոդը կանչվում է, երբ component-ը տեղադրվում է DOM-ում: Այն սովորաբար օգտագործվում է այնպիսի գործողություններ կատարելու համար, որոնք պահանջում են փոխազդեցություն բրաուզերի կամ արտաքին API-ների հետ, ինչպես օրինակ` սերվերից տվյալներ ստանալը: Այս մեթոդը իդեալական վայր է սկզբանվորման(initialization) և տեղադրման համար:<br>Օրինակ.</p><pre>class MyComponent extends React.Component {<br>  componentDidMount() {<br>    // Fetch data from a server or perform other side effects<br>  }<br>}</pre><h3>Component Updating Phase</h3><p>Այս փուլը տեղի է ունենում, երբ component-ի state-ը կամ props-ը փոխվում է, և component-ը պետք է թարմացվի DOM-ում:</p><blockquote><strong><em>Updating</em></strong><em> փուլի կյանքի ցիկլի մեթոդները լինում են`<br></em>1) The <strong><em>shouldComponentUpdate()</em></strong><em> </em>lifecycle method<br>2) The <strong><em>getDerivedStateFromProps()</em></strong> lifecycle method<br>3) The <strong><em>render()</em></strong> lifecycle method<br>4) The <strong><em>componentDidUpdate()</em></strong> lifecycle method</blockquote><blockquote>The shouldComponentUpdate( ) lifecycle method</blockquote><p><strong>shouldComponentUpdate( ) </strong>մեթոդը կանչվում է նախքան component-ի թարմացումը: Ստանում է երկու արգումենտ` nextProps և nextState: Այս մեթոդը վերադարձնում է բուլյան արժեք, որը ցույց է տալիս component-ը եթե թարմացվել է `<strong><em>true</em></strong> , եթե ոչ `<strong><em>false</em></strong>:<br>Օրինակ.</p><pre>class MyComponent extends React.Component {<br>  shouldComponentUpdate(nextProps, nextState) {<br>    // Compare current props and state with nextProps and nextState<br>    // Return true if the component should update, false otherwise<br>  }<br>}</pre><blockquote>The getDerivedStateFromProps( ) lifecycle method</blockquote><p><strong>getDerivedStateFromProps( ) </strong>մեթոդը կանչվում նաև Updating փուլում, որը նման է Mounting փուլին: Ստանում է երկու արգումենտ` <strong><em>props</em></strong> և <strong><em>state</em></strong>: Վերադարձնում է օբյեկտ, որը ներկայացնում է component-ի թարմացված state-ը կամ <strong>null ` </strong>եթե state-ի թարմացում չկա:<br>Օրինակ.</p><pre>class MyComponent extends React.Component {<br>  static getDerivedStateFromProps(props, state) {<br>    // Update the state based on changes in props<br>    // Return an object representing the updated state or null<br>  }<br>}</pre><blockquote>The render( ) lifecycle method</blockquote><p><strong>render( ) </strong>մեթոդը կրկին կանչվում է Updating փուլում` component-ի UI-ը ցուցադրելու համար` հիմնված թարմացված <strong><em>props</em></strong>-ի և <strong><em>state</em></strong>-ի վրա:<br>Օրինակ.</p><pre>class MyComponent extends React.Component {<br>  render() {<br>    return &lt;div&gt;{this.props.message}&lt;/div&gt;;<br>  }<br>}</pre><blockquote>The componentDidUpdate( ) lifecycle method</blockquote><p><strong>componentDidUpdate( )</strong> մեթոդը կանչվում է component-ի թարմացումից և re-rendering-ից հետո: Այն օգտակար է լրացուցիչ գործողություններ կատարելու համար, երբ component-ի props-ը և state-ը փոխվել են: Այս մեթոդի շրջանականերում կարող եք համեմատել նախորդ(<strong><em>previous</em></strong>) և ընթացիկ(<strong><em>current</em></strong>) props-ն ու state-ը, անհրաժեշտության դեպքում նաև թարմացնել component-ը: Այնուամենայնիվ, այս մեթոդով component-ի state-ը թարմացնելիս պետչ է զգույշ լինել` անվերջ թարմացման ցիկլերից խուսափելու համար:<br>Օրինակ.</p><pre>class MyComponent extends React.Component {<br>  componentDidUpdate(prevProps, prevState) {<br>    // Perform actions based on changes in props or state<br>  }<br>}</pre><h3>Component Unmounting Phase</h3><p>Այս փուլը React component-ի lifecycle-ի վերջին փուլն է: Այն տեղի է ունենում այն ժամանակ, երբ component-ը հեռացվում է DOM-ից:</p><blockquote><strong><em>Unmounting</em></strong><em> փուլի կյանքի ցիկլի միակ մեթոդը հետևյալն է`<br></em>1) The <strong><em>componentWillUnmount()</em></strong><em> </em>lifecycle method</blockquote><blockquote>The componentWillUnmount( ) lifecycle method</blockquote><p><strong>componentWillUnmount( )</strong> մեթոդը կանչելուց հետո component-ը հանվում է DOM-ից, և դրա բոլոր state-երն ու props-ներն ոչնչացվում են: Այն թույլ է տալիս կատարել ցանկացած անհրաժեշտ մաքրում, ինչպիսիք են ` ժամանակաչափերի չեղարկումը(canceling timers), event listeners հեռացումը կամ Mounting փուլում կազմաձևած տվյալների կառուցվածքների(data structures) մաքրումը: Այս մեթոդը կարևոր է հիշողության արտահոսքը կանխելու և պատշաճ մաքրումն ապահովելու համար:<br>Օրինակ.</p><pre>class MyComponent extends React.Component {<br>  componentWillUnmount() {<br>    // Clean up resources, cancel timers, close connections, etc.<br>  }<br>}</pre><h3>Օգտագործման օրինակ`</h3><p>Եկեք դիտարկենք մի կոդ, որտեղ մենք ունենք <strong><em>Timer</em></strong> կոչվող component, որը ցուցադրում է ժամանակաչափը և ամեն վայրկյան թարմացնում է դրա state-ը: Մենք կօգտագործենք lifecycle-ի տարբեր մեթոդներ՝ ժամանակաչափը արժեքավորելու, այն գործարկելու, ռեսուրսները թարմացնելու և մաքրելու համար` երբ component-ն ապամոնտաժված է:</p><pre>class Timer extends React.Component {<br>  constructor(props) {<br>    super(props);<br>    this.state = {<br>      count: 0<br>    };<br>  }</pre><pre>  componentDidMount() {<br>    this.timerId = setInterval(() =&gt; {<br>      this.setState((prevState) =&gt; ({<br>        count: prevState.count + 1<br>      }));<br>    }, 1000);<br>  }</pre><pre>  componentDidUpdate(prevProps, prevState) {<br>    if (prevState.count !== this.state.count) {<br>      // Perform actions specific to count updates<br>    }<br>  }</pre><pre>  componentWillUnmount() {<br>    clearInterval(this.timerId);<br>  }</pre><pre>  render() {<br>    return &lt;div&gt;{this.state.count}&lt;/div&gt;;<br>  }<br>}</pre><p>Այս օրինակում մենք արժեքավորում ենք ժամանակաչափը componentDiDMount( ) մեթոդում` սահմանելով միջակայք` state-ն ամեն վայրկյան թարմացնելու համար: Մենք նաև համեմատում ենք <strong><em>previous</em></strong> և <strong><em>current</em></strong> state-ը componentDidUpdate( )-ում՝ թարմացումները հաշվելու և հատուկ գործողություններ կատարելու համար: Իսկ վերջում, մենք մաքրում ենք componentDiDMount( )-ի միջակայքը` հիշողության արտահոսքը կանխելու համար:</p><p>Օգտագործելով համապատասխան կյանքի ցիկլի մեթոդներ, մենք կարող ենք վերահսկել component-ների state-ը և կատարել անհրաժեշտ գործողություններ ճիշտ ժամանակին ` իրենց lifecycle-ի ընթացքում: Այս մեթոդների ըմբռնումը մեզ թույլ է տալիս օպտիմալացնել աշխատանքը, կառավարել state-ը և արդյունավետ կերպով կարգավորել ռեսուրսների մաքրումը:</p><p>Կարևոր է նշել, որ React Hooks-ի ներդրմամբ React-ի նոր տարբերակներում, class-ի վրա հիմնված component-ների և lifecycle-ի մեթոդների օգտագործումը հիմնականում փոխարինվել է ֆունկցիոնալ component-ներով և hook-երով: Այնուամենայնիվ` class-ի վրա հիմնված component-ները դեռ արդիական են և լայնորեն օգտագործվում են:</p><p><strong>Ամփոփելով, </strong>React Component Lifecycle մեթոդները թույլ են տալիս միանալ component-ի lifecycle-ի տարբեր փուլերին և կատարել որոշակի գործողություններ: Օգտագործելով այս մեթոդները` մենք կարող ենք կառավարել component-ի state-ը, օպտիմալացնել կատարումը և ապահովել ռեսուրսների պատշաճ մաքրում:</p><p><strong>Շնորհակալություն կարդալու համար…<br>Հուսով եմ, որ այս հոդվածը ձեզ համար օգտակար էր:</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e3b503c5490" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>