react
Oct 4, 2025
3
1 views

React Component Lifecycle အကြောင်း

Appician
Writer
React Component Lifecycle အကြောင်း

React မှာ component ရဲ့ lifecycle ကို အောက်မှာပြထားတဲ့အတိုင်း အဓိကအားဖြင့် Mounting, Updating, Unmounting ဆိုပြီး အပိုင်း (၃) ခွဲခြားနိုင်ပါတယ်။ အပိုင်းတစ်ခုချင်းစီမှာ ကိုယ်ပိုင် lifecycle method (functional component မှာဆိုရင် Hook) ရှိကြပါတယ်။

React Component Lifecycle

Mounting

ဒီအပိုင်းမှာရှိတဲ့ lifecycle method တွေက component ကို create လုပ်ပြီးတဲ့အချိန်နှင့် DOM (Document Object Model) ထဲ ထည့်ပြီးတဲ့အချိန်မှာ run ပါတယ်။ သူ့ရဲ့ lifecycle method တွေကတော့

  • constructor()
  • static getDerivedStateFromProps()
  • render()
  • componentDidMount() တို့ပဲ ဖြစ်ပါတယ်။

React Component Lifecycle - Mounting Phase

ဥပမာ -

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
    console.log('Constructor: Initializing state');
  }

  static getDerivedStateFromProps(nextProps, prevState) {
    console.log('Derived State: Syncing state with props');
    return null; // state အသစ် return ပြန် (သို့) state က ဘာမှမပြောင်းလဲဘူးဆိုရင် null ကို return ပြန်
  }

  componentDidMount() {
    console.log('Component Did Mount: Component has been rendered');
    // data များ fetch လုပ်ခြင်း နှင့် subscription လုပ်ခြင်းတွေကို ဒီနေရာမှာ လုပ်ရပါတယ်။
  }

  render() {
    console.log('Render: Rendering component');
    return <div>Count: {this.state.count}</div>;
  }
}
;
Updating

ဒီအပိုင်းမှာရှိတဲ့ lifecycle method တွေကတော့ prop သို့မဟုတ် state တစ်ခုခုကြောင့် component တွေ re-render လုပ်ပြီးတဲ့အချိန်မှာ run ပါတယ်။ သူ့ရဲ့ lifecycle method တွေကတော့

  • static getDerivedStateFromProps()
  • shouldComponentUpdate()
  • render()
  • getSnapshotBeforeUpdate()
  • componentDidUpdate() တို့ပဲ ဖြစ်ပါတယ်။

React Component Lifecycle - Updating Phase

ဥပမာ -

class MyComponent extends React.Component {
  state = { count: 0 };

  static getDerivedStateFromProps(nextProps, prevState) {
    console.log('Derived State: Syncing state with props');
    return null;
  }

  shouldComponentUpdate(nextProps, nextState) {
    console.log('Should Component Update: Deciding whether to re-render');
    return true; //update လုပ်မယ်ဆိုရင် true ကို return ပြန်ပြီးတော့ မလုပ်ဘူးဆိုရင် false ကို return ပြန်
  }

  getSnapshotBeforeUpdate(prevProps, prevState) {
    console.log('Snapshot Before Update: Capturing some information');
    return null; //componentDidUpdate မှာ pass လုပ်မယ့် value ကို return ပြန်
  }

  componentDidUpdate(prevProps, prevState, snapshot) {
    console.log('Component Did Update: Component has updated');
    // previous state/props ပေါ်မူတည်ပြီး operation များ လုပ်ဆောင်
  }

  render() {
    console.log('Render: Rendering component');
    return <button onClick={() => this.setState({ count: this.state.count + 1 })}>Increment</button>;
  }
}
;
Unmounting

ဒီအပိုင်းမှာရှိတဲ့ lifecycle method တွေက component ကို DOM မှ remove လုပ်ပြီးတဲ့အချိန်မှာ run ပါတယ်။ သူ့ရဲ့ lifecycle method ကတော့

  • componentWillUnmount()

ဥပမာ -

class MyComponent extends React.Component {
  componentWillUnmount() {
    console.log('Component Will Unmount: Cleanup tasks here');
    // timer တွေကို clear လုပ်ခြင်း, network request တွေကို cancel လုပ်ခြင်းများ ဒီနေရာမှာ လုပ်ဆောင်နိုင်
  }

  render() {
    return <div>Goodbye!</div>;
  }
}
;
Functional Components and Hooks

Hook များကို အသုံးပြုပြီးတော့လည်း lifecycle event တွေကို ကိုင်တွယ်လို့ရပါတယ်။

Handling Lifecycle event in Hook

ဥပမာ -

import React, { useState, useEffect } from 'react';

const MyComponent = () => {
  const [count, setCount] = useState(0);

  useEffect(() => {
    console.log('Component Did Mount: Component has been rendered');
    // data များ fetch လုပ်ခြင်း နှင့် subscription လုပ်ခြင်းတွေကို ဒီနေရာမှာ လုပ်ရပါတယ်။

    return () => {
      console.log('Component Will Unmount: Cleanup tasks here');
    };
  }, []); // Empty array ကတော့ component ကို mount တဲ့အချိန်မှာ ၁ ကြိမ် run ပြီး unmount တဲ့အချိန်မှာ cleanup function ကို ၁ ကြိမ် run   

  useEffect(() => {
    console.log('Component Did Update: Count changed to', count);
  }, [count]); // count ရဲ့ တန်ဖိုး ပြောင်းလဲတဲ့အချိန်တိုင်းမှာ run

  return (
    <button onClick={() => setCount(count + 1)}>
      Increment: {count}
    </button>
  );
};;

Component Lifecycle ကို သေချာနားလည်မယ်ဆိုရင် side effect များ၊ data fetch လုပ်ခြင်းများ၊ cleanup လုပ်ခြင်းများကို သေချာစွာ လုပ်ဆောင်စေနိုင်မှာပဲ ဖြစ်ပါတယ်။

အပိုင်းတစ်ခုချင်းစီရဲ့ Lifecycle Method တွေအကြောင်းကိုတော့ နောက်ထပ်ရေးမယ့် ဆောင်းပါးတွေမှာ အသေးစိတ်ရှင်းပြပေးပါမယ်။

Tags

#react#typescript
1
Views
1
Likes
1
Comments

Related Articles

React useReducer Hook အကြောင်း
react
May 9, 2024
7

React useReducer Hook အကြောင်း

Reducer ဆိုတဲ့စကားလုံးက value နှစ်ခုကိုလက်ခံပြီး value တစ်ခုကို return ပြန်တဲ့ function ကိုခေါ်တဲ့ အခေါ်အဝေါ်တစ်ခုဖြစ်ပါတယ်။ Array တစ်ခုမှာရှိတဲ့ value တွေအားလုံးကို value တစ်ခုတည်းမှာ ပေါင်းစပ်ချင်တဲ့အခါ Array ရဲ့ function တစ်ခုဖြစ်တဲ့ reduce function ကိုသုံးလို့ရပါတယ်။ အောက်မှာဖော်ပြထားတဲ့အတိုင်း reducer function တစ်ခုရေးပြီး reduce ထဲမှာ pass လုပ်လို့ရပါတယ်။

Read Article
React useRef Hook အကြောင်း
react
Oct 4, 2025
7

React useRef Hook အကြောင်း

useRef Hook ဟာ သူ့ရဲ့ argument (initialValue) ကို .current ဆိုတဲ့ သူ့ရဲ့ property မှာ initialized လုပ်ပြီး multable ref object ကို return ပြန်တဲ့ function တစ်ခုဖြစ်ပါတယ်။ သူ return ပြန်တဲ့ object ဟာ component ရဲ့ lifetime တစ်လျှောက်လုံးမှာ persist (ဆက်လက်တည်ရှိ) နေပါလိမ့်မည်။

Read Article
React.useCallback Hook အသုံးပြုပုံ
react
Oct 4, 2025

React.useCallback Hook အသုံးပြုပုံ

Function object တစ်ခုက တခြား function တစ်ခုကို return ပြန်လို့ရပါတယ်။ (factory() function က လုပ်သလိုမျိုး)။ ပြီးတော့ အဲဒီ function တစ်ခုနှင့်တစ်ခု နှိုင်းယှဉ်လို့ရပါတယ်။ Object တစ်ခုက လုပ်လို့ရသမျှအကုန်လုံး လုပ်လို့ရပါတယ်။

Read Article

Enjoyed this article?

Subscribe to get notified when I publish new articles about web development, React, and modern JavaScript.

1