အတန်း၏ အစိတ်အပိုင်းများကို တုံ့ပြန်ပါ။
React 16.8 မတိုင်မီ၊ Class အစိတ်အပိုင်းများသည် React အစိတ်အပိုင်းတစ်ခုရှိ အခြေအနေနှင့် ဘဝသံသရာကို ခြေရာခံရန် တစ်ခုတည်းသောနည်းလမ်းဖြစ်သည်။ လုပ်ဆောင်ချက် အစိတ်အပိုင်းများကို "state-less" ဟု သတ်မှတ်ခဲ့သည်။
Hooks များ ပေါင်းထည့်ခြင်းဖြင့် Function အစိတ်အပိုင်းများသည် ယခုအခါ Class အစိတ်အပိုင်းများနှင့် တူညီလုနီးပါးဖြစ်သည်။ ကွာခြားချက်များသည် အလွန်သေးငယ်သောကြောင့် React တွင် Class အစိတ်အပိုင်းကို သင်အသုံးပြုရန် မလိုအပ်တော့ပါ။
Function အစိတ်အပိုင်းများကို ဦးစားပေးသော်လည်း၊ React မှ Class အစိတ်အပိုင်းများကို ဖယ်ရှားရန် လက်ရှိအစီအစဉ်မရှိပါ။
ဤအပိုင်းသည် React တွင် Class အစိတ်အပိုင်းများကို အသုံးပြုနည်းကို ခြုံငုံသုံးသပ်ပေးပါမည်။
ဤကဏ္ဍကို ကျော်သွား၍ Function Components များအစား အသုံးပြုပါ။
အစိတ်အပိုင်းများကို တုံ့ပြန်ပါ။
အစိတ်အပိုင်းများသည် သီးခြားလွတ်လပ်ပြီး ပြန်သုံးနိုင်သော ကုဒ်များဖြစ်သည်။ ၎င်းတို့သည် JavaScript လုပ်ဆောင်ချက်များကဲ့သို့ တူညီသောရည်ရွယ်ချက်ကို လုပ်ဆောင်သော်လည်း သီးခြားလုပ်ဆောင်ပြီး HTML ကို render() လုပ်ဆောင်ချက်မှတစ်ဆင့် ပြန်ပေးသည်။
အစိတ်အပိုင်းများသည် Class components နှင့် Function components ဟူ၍ နှစ်မျိုးရှိပြီး၊ ဤအခန်းတွင် Class အစိတ်အပိုင်းများအကြောင်းကို လေ့လာပါမည်။
Class Component တစ်ခုဖန်တီးပါ။
React အစိတ်အပိုင်းကို ဖန်တီးသောအခါ၊ အစိတ်အပိုင်း၏အမည်သည် စာလုံးကြီးဖြင့် စတင်ရပါမည်။
အစိတ်အပိုင်းသည် extends React.Component
ထုတ်ပြန်ချက်တွင် ပါဝင်ရမည်၊ ဤထုတ်ပြန်ချက်သည် React.Component ၏ အမွေဆက်ခံမှုကို ဖန်တီးပေးပြီး သင့်အစိတ်အပိုင်းအား React.Component ၏ လုပ်ဆောင်ချက်များကို ဝင်ရောက်ခွင့်ပေးသည်။
အစိတ်အပိုင်းသည် render()
နည်းလမ်းတစ်ခုလည်း လိုအပ်သည်၊ ဤနည်းလမ်းသည် HTML ကို ပြန်ပေးသည်။
ဥပမာ
ဟုခေါ်သော Class အစိတ်အပိုင်းတစ်ခုကို ဖန်တီးပါ။Car
class Car extends React.Component {
render() {
return <h2>Hi, I am a Car!</h2>;
}
}
ယခု သင်၏ React အပလီကေးရှင်းတွင် ဒြပ်စင်တစ်ခုကို ပြန်ပေးသည့် Car ဟုခေါ်သော အစိတ်အပိုင်းတစ်ခုရှိသည်
<h2>
။
သင့်အပလီကေးရှင်းတွင် ဤအစိတ်အပိုင်းကို အသုံးပြုရန်၊ သာမန် HTML ကဲ့သို့ အလားတူ syntax ကို အသုံးပြုပါ-
<Car />
ဥပမာ
Car
"အမြစ်" ဒြပ်စင်ရှိ အစိတ်အပိုင်းကို ပြ ပါ-
ReactDOM.render(<Car />, document.getElementById('root'));
လက်မှတ်ရယူပါ။
$95 စာရင်းသွင်းပါ။
အစိတ်အပိုင်းတည်ဆောက်သူ
သင်၏ အစိတ်အပိုင်းတွင် လုပ်ဆောင်ချက် တစ်ခု ရှိပါက
၊ အစိတ်အပိုင်းကို စတင်သောအခါတွင် ဤလုပ်ဆောင်ချက်ကို ခေါ်ပါမည်။constructor()
constructor function သည် အစိတ်အပိုင်း၏ ဂုဏ်သတ္တိများကို သင်စတင်သည့်နေရာဖြစ်သည်။
တုံ့ပြန်မှုတွင်၊ အစိတ်အပိုင်းဂုဏ်သတ္တိများကို ခေါ်သည့် အရာဝတ္ထုတစ်ခုတွင် သိမ်းဆည်းထားသင့်သည်
state
။
state
ဤသင်ခန်းစာတွင် နောက်ပိုင်းတွင် သင်အကြောင်းပိုမိုလေ့လာနိုင်ပါမည် ။
super()
constructor function သည် parent component ၏ constructor function ကိုလုပ်ဆောင်ပေးသည့် statement အပါအဝင် parent component ၏ အမွေဆက်ခံမှုကို ဂုဏ်ပြုသည့်နေရာတွင်ရှိပြီး ၊ သင်၏ component သည် parent component ( React.Component
) ၏ function အားလုံးကို အသုံးပြုခွင့်ရှိသည်။
ဥပမာ
ကားအစိတ်အပိုင်းတွင် constructor function ကိုဖန်တီးပြီး အရောင်ပိုင်ဆိုင်မှုတစ်ခုထည့်ပါ-
class Car extends React.Component {
constructor() {
super();
this.state = {color: "red"};
}
render() {
return <h2>I am a Car!</h2>;
}
}
render() လုပ်ဆောင်ချက်တွင် အရောင်ပိုင်ဆိုင်မှုကို အသုံးပြုပါ-
ဥပမာ
class Car extends React.Component {
constructor() {
super();
this.state = {color: "red"};
}
render() {
return <h2>I am a {this.state.color} Car!</h2>;
}
}
ကျားကန်
အစိတ်အပိုင်းဂုဏ်သတ္တိများကို ကိုင်တွယ်ခြင်း၏ နောက်ထပ်နည်းလမ်းမှာ အသုံးပြုခြင်းဖြင့် props
ဖြစ်သည်။
Props သည် function arguments များကဲ့သို့ဖြစ်ပြီး ၎င်းတို့ကို attributes အဖြစ် အစိတ်အပိုင်းထဲသို့ သင်ပေးပို့ပါသည်။
props
နောက်အခန်းတွင် သင်ပိုမိုလေ့လာသင်ယူလိမ့်မည် ။
ဥပမာ
ကားအစိတ်အပိုင်းသို့ အရောင်တစ်ခုပေးပို့ရန် ရည်ညွှန်းချက်တစ်ခုကို အသုံးပြုပြီး render() လုပ်ဆောင်ချက်တွင် အသုံးပြုပါ-
class Car extends React.Component {
render() {
return <h2>I am a {this.props.color} Car!</h2>;
}
}
ReactDOM.render(<Car color="red"/>, document.getElementById('root'));
Constructor တွင် Pros များ
အကယ်၍ သင့်အစိတ်အပိုင်းတွင် constructor function ပါရှိပါက၊ props များကို constructor သို့ အမြဲတမ်း method မှတဆင့် React.Component သို့ ပေးပို့သင့် super()
ပါသည်။
ဥပမာ
class Car extends React.Component {
constructor(props) {
super(props);
}
render() {
return <h2>I am a {this.props.model}!</h2>;
}
}
ReactDOM.render(<Car model="Mustang"/>, document.getElementById('root'));
အစိတ်အပိုင်းများတွင် အစိတ်အပိုင်းများ
ကျွန်ုပ်တို့သည် အခြားသော အစိတ်အပိုင်းများအတွင်းရှိ အစိတ်အပိုင်းများကို ကိုးကားနိုင်သည်-
ဥပမာ
Garage အစိတ်အပိုင်းအတွင်းရှိ ကားအစိတ်အပိုင်းကို အသုံးပြုပါ-
class Car extends React.Component {
render() {
return <h2>I am a Car!</h2>;
}
}
class Garage extends React.Component {
render() {
return (
<div>
<h1>Who lives in my Garage?</h1>
<Car />
</div>
);
}
}
ReactDOM.render(<Garage />, document.getElementById('root'));
Files ရှိ အစိတ်အပိုင်းများ
React သည် ကုဒ်ကို ပြန်လည်အသုံးပြုခြင်းအကြောင်းဖြစ်ပြီး၊ သီးခြားဖိုင်များတွင် သင့်အစိတ်အပိုင်းအချို့ကို ထည့်သွင်းရန် စမတ်ကျနိုင်သည်။
ထိုသို့ပြုလုပ်ရန်၊ ဖိုင်တိုးချဲ့မှုတစ်ခုဖြင့် ဖိုင်အသစ်တစ်ခုကို ဖန်တီးပြီး ၎င်း .js
အတွင်းကုဒ်ကို ထည့်ပါ။
ဖိုင်သည် React (ယခင်အတိုင်း) တင်သွင်းခြင်းဖြင့် စတင်ရမည်ဖြစ်ပြီး ၎င်းသည် ထုတ်ပြန်ချက်နှင့် အဆုံးသတ်ရမည်ဖြစ်ကြောင်း သတိပြုပါ export default Car;
။
ဥပမာ
ဤသည်မှာ ဖိုင်အသစ်ဖြစ်ပြီး Car.js
၊
import React from 'react';
class Car extends React.Component {
render() {
return <h2>Hi, I am a Car!</h2>;
}
}
export default Car;
အစိတ်အပိုင်း ကိုအသုံးပြုရန် Car
၊ သင်သည်သင်၏လျှောက်လွှာတွင်ဖိုင်ကိုတင်သွင်းရမည်ဖြစ်သည်။
ဥပမာ
ယခု ကျွန်ုပ်တို့သည် Car.js
အပလီကေးရှင်းတွင် ဖိုင်ကို
တင်သွင်းပြီး Car
ဤနေရာတွင် ဖန်တီးထားသကဲ့သို့ အစိတ်အပိုင်းကို ကျွန်ုပ်တို့အသုံးပြုနိုင်ပါသည်။
import React from 'react';
import ReactDOM from 'react-dom';
import Car from './Car.js';
ReactDOM.render(<Car />, document.getElementById('root'));
Class Component State ကို တုံ့ပြန်ပါ။
React Class အစိတ်အပိုင်းများတွင် built-in state
object တစ်ခုရှိသည်။
state
အစိတ်အပိုင်းတည်ဆောက်မှုကဏ္ဍတွင် အစောပိုင်းက ကျွန်ုပ်တို့ အသုံးပြုခဲ့သည်ကို သင်သတိပြုမိပေမည် ။
state
အရာဝတ္ထုသည် အစိတ်အပိုင်းနှင့် သက်ဆိုင်သည့် ပိုင်ဆိုင်မှုတန်ဖိုးများကို သင်သိမ်းဆည်းသည့်နေရာဖြစ်သည် ။
အ state
ရာဝတ္တုပြောင်းလဲသောအခါ၊ အစိတ်အပိုင်းသည် ပြန်လည်ဖော်ပြသည်။
State Object ကို ဖန်တီးခြင်း။
အခြေအနေအရာဝတ္တုကို တည်ဆောက်သူတွင် စတင်လုပ်ဆောင်သည်-
ဥပမာ
state
တည်ဆောက်သူနည်းလမ်းတွင် အရာဝတ္တုကို သတ်မှတ်ပါ -
class Car extends React.Component {
constructor(props) {
super(props);
this.state = {brand: "Ford"};
}
render() {
return (
<div>
<h1>My Car</h1>
</div>
);
}
}
အခြေအနေအရာဝတ္ထုတွင် သင်နှစ်သက်သလောက် ဂုဏ်သတ္တိများစွာ ပါဝင်နိုင်သည်-
ဥပမာ
သင်၏ အစိတ်အပိုင်း လိုအပ်သော ဂုဏ်သတ္တိများ အားလုံးကို သတ်မှတ်ပါ-
class Car extends React.Component {
constructor(props) {
super(props);
this.state = {
brand: "Ford",
model: "Mustang",
color: "red",
year: 1964
};
}
render() {
return (
<div>
<h1>My Car</h1>
</div>
);
}
}
state
Object ကိုအသုံးပြုခြင်း။
အထားအ သို state
ကိုအသုံးပြုခြင်းဖြင့် အစိတ်အပိုင်းအတွင်းရှိ မည်သည့်အရာဝတ္ထု
ကိုမဆို ကိုးကားပါ -this.state.propertyname
ဥပမာ-
နည်းလမ်း state
ရှိ အရာဝတ္ထုကို
ကိုးကားပါ -render()
class Car extends React.Component {
constructor(props) {
super(props);
this.state = {
brand: "Ford",
model: "Mustang",
color: "red",
year: 1964
};
}
render() {
return (
<div>
<h1>My {this.state.brand}</h1>
<p>
It is a {this.state.color}
{this.state.model}
from {this.state.year}.
</p>
</div>
);
}
}
state
Object ကိုပြောင်းလဲခြင်း။
အခြေအနေအရာဝတ္တုတွင် တန်ဖိုးတစ်ခုပြောင်းရန်၊ this.setState()
နည်းလမ်းကို အသုံးပြုပါ။
အရာဝတ္တု ရှိတန်ဖိုးတစ်ခု state
ပြောင်းလဲသောအခါ၊ အစိတ်အပိုင်းသည် ပြန်လည်ဖော်ပြလိမ့်မည်၊ ဆိုလိုသည်မှာ အထွက်တန်ဖိုးအသစ်(များ) အရ ပြောင်းလဲသွားမည်ဖြစ်သည်။
ဥပမာ-
onClick
အရောင်ပိုင်ဆိုင်မှုကို ပြောင်းလဲမည့် ဖြစ်ရပ်တစ်ခု နှင့်အတူ ခလုတ်တစ်ခုကို ထည့်ပါ -
class Car extends React.Component {
constructor(props) {
super(props);
this.state = {
brand: "Ford",
model: "Mustang",
color: "red",
year: 1964
};
}
changeColor = () => {
this.setState({color: "blue"});
}
render() {
return (
<div>
<h1>My {this.state.brand}</h1>
<p>
It is a {this.state.color}
{this.state.model}
from {this.state.year}.
</p>
<button
type="button"
onClick={this.changeColor}
>Change color</button>
</div>
);
}
}
အခြေအနေအရာဝတ္တုကို ပြောင်းလဲရန် နည်းလမ်းကို အမြဲသုံးပါ setState()
၊ ၎င်းသည် အစိတ်အပိုင်းအား ၎င်း၏ မွမ်းမံပြင်ဆင်ထားသည်ကို သိပြီး render() နည်းလမ်း (နှင့် အခြားသော ဘဝသံသရာနည်းလမ်းများအားလုံးကို) ခေါ်ဆိုကြောင်း သေချာစေမည်ဖြစ်သည်။
အစိတ်အပိုင်းများ၏ဘဝသံသရာ
React ရှိ အစိတ်အပိုင်းတစ်ခုစီတွင် ၎င်း၏ပင်မအဆင့်သုံးဆင့်အတွင်း သင်စောင့်ကြည့်ပြီး စီမံခန့်ခွဲနိုင်သည့် ဘဝသံသရာရှိသည်။
အဆင့်သုံးဆင့်မှာ- Mounting ၊ Update နှင့် Unmounting တို့ ဖြစ်သည်။
တပ်ဆင်ခြင်း။
Mounting ဆိုသည်မှာ DOM ထဲသို့ ဒြပ်စင်များထည့်ခြင်းကို ဆိုလိုသည်။
React တွင် အစိတ်အပိုင်းတစ်ခုကို တပ်ဆင်သည့်အခါတွင် ဤအစီအစဥ်အတိုင်း ခေါ်ဝေါ်သည့် ပေါင်းစပ်ထည့်သွင်းနည်း လေးခုပါရှိသည်-
constructor()
getDerivedStateFromProps()
render()
componentDidMount()
render()
နည်းလမ်းသည် လိုအပ်ပြီး အမြဲခေါ်မည်၊ အခြားနည်းလမ်းများကို ရွေးချယ်နိုင်ပြီး ၎င်းတို့ကို သင်သတ်မှတ်ပါက ၎င်းတို့ကို ခေါ်ပါမည် ။
constructor
အစိတ်အပိုင်းကို အစပြုသောအခါ အခြားအရာများရှေ့တွင် အဆိုပါ constructor()
နည်းလမ်းကို ခေါ်ပြီး ကနဦး state
နှင့် အခြားကနဦးတန်ဖိုးများကို သတ်မှတ်ရန် သဘာဝနေရာဖြစ်သည်။
constructor()
နည်းလမ်းကို , အငြင်းအခုံများအဖြစ် ခေါ်ဝေါ်
ပြီး props
အခြား super(props)
အရာများမတိုင်မီ သင်ခေါ်ဆိုခြင်းဖြင့် အမြဲစတင်သင့်သည်၊ ၎င်းသည် parent ၏ constructor method ကို စတင်မည်ဖြစ်ပြီး အစိတ်အပိုင်းအား ၎င်း၏ parent ( React.Component
) မှ method များကို အမွေဆက်ခံခွင့်ပေးမည်ဖြစ်သည်။
ဥပမာ-
အစိတ်အပိုင်းတစ်ခုကို သင်ပြုလုပ် တိုင်း constructor
၊ နည်းလမ်းကို React ဟုခေါ်သည်-
class Header extends React.Component {
constructor(props) {
super(props);
this.state = {favoritecolor: "red"};
}
render() {
return (
<h1>My Favorite Color is {this.state.favoritecolor}</h1>
);
}
}
ReactDOM.render(<Header />, document.getElementById('root'));
getDerivedStateFromProps
DOM တွင် ဒြပ်စင်(များ) ကို မဖော်ပြ မီ getDerivedStateFromProps()
နည်းလမ်းကို မှန်ကန်စွာ ခေါ်သည်။
state
ဤသည်မှာ မူလအစကိုအခြေခံ၍ အရာဝတ္ထု
ကို သတ်မှတ်ရန် သဘာဝနေရာဖြစ်သည် props
။
It takes
state
as an argument, and returns an object with changes to the
state
.
The example below starts with the favorite color being
"red", but the
getDerivedStateFromProps()
method updates the favorite color based on the
favcol
attribute:
Example:
The getDerivedStateFromProps
method is called
right before the render method:
class Header extends React.Component {
constructor(props) {
super(props);
this.state = {favoritecolor: "red"};
}
static getDerivedStateFromProps(props, state) {
return {favoritecolor: props.favcol };
}
render() {
return (
<h1>My Favorite Color is {this.state.favoritecolor}</h1>
);
}
}
ReactDOM.render(<Header favcol="yellow"/>, document.getElementById('root'));
render
The render()
method is required, and is the
method that actually outputs the HTML to the DOM.
Example:
A simple component with a simple render()
method:
class Header extends React.Component {
render() {
return (
<h1>This is the content of the Header component</h1>
);
}
}
ReactDOM.render(<Header />, document.getElementById('root'));
componentDidMount
The componentDidMount()
method is called after the
component is rendered.
This is where you run statements that requires that the component is already placed in the DOM.
Example:
At first my favorite color is red, but give me a second, and it is yellow instead:
class Header extends React.Component {
constructor(props) {
super(props);
this.state = {favoritecolor: "red"};
}
componentDidMount() {
setTimeout(() => {
this.setState({favoritecolor: "yellow"})
}, 1000)
}
render() {
return (
<h1>My Favorite Color is {this.state.favoritecolor}</h1>
);
}
}
ReactDOM.render(<Header />, document.getElementById('root'));
Updating
The next phase in the lifecycle is when a component is updated.
A component is updated whenever there is a change in the component's
state
or props
.
React has five built-in methods that gets called, in this order, when a component is updated:
getDerivedStateFromProps()
shouldComponentUpdate()
render()
getSnapshotBeforeUpdate()
componentDidUpdate()
The render()
method is required and will
always be called, the others are optional and will be called if you define them.
getDerivedStateFromProps
Also at updates the getDerivedStateFromProps
method is
called. This is the first method that is called when a component gets updated.
This is still the natural place to set the state
object based on the initial props.
The example below has a button that changes the favorite color to blue, but
since the getDerivedStateFromProps()
method is called,
which updates the state with the color from the favcol attribute, the favorite color is
still
rendered as yellow:
Example:
If the component gets updated, the getDerivedStateFromProps()
method is called:
class Header extends React.Component {
constructor(props) {
super(props);
this.state = {favoritecolor: "red"};
}
static getDerivedStateFromProps(props, state) {
return {favoritecolor: props.favcol };
}
changeColor = () => {
this.setState({favoritecolor: "blue"});
}
render() {
return (
<div>
<h1>My Favorite Color is {this.state.favoritecolor}</h1>
<button type="button" onClick={this.changeColor}>Change color</button>
</div>
);
}
}
ReactDOM.render(<Header favcol="yellow"/>, document.getElementById('root'));
shouldComponentUpdate
In the shouldComponentUpdate()
method
you can return a Boolean value that specifies whether React should continue with the rendering or not.
The default value is true
.
The example below shows what happens when the
shouldComponentUpdate()
method returns false
:
Example:
Stop the component from rendering at any update:
class Header extends React.Component {
constructor(props) {
super(props);
this.state = {favoritecolor: "red"};
}
shouldComponentUpdate() {
return false;
}
changeColor = () => {
this.setState({favoritecolor: "blue"});
}
render() {
return (
<div>
<h1>My Favorite Color is {this.state.favoritecolor}</h1>
<button type="button" onClick={this.changeColor}>Change color</button>
</div>
);
}
}
ReactDOM.render(<Header />, document.getElementById('root'));
Example:
Same example as above, but this time the shouldComponentUpdate()
method returns
true
instead:
class Header extends React.Component {
constructor(props) {
super(props);
this.state = {favoritecolor: "red"};
}
shouldComponentUpdate() {
return true;
}
changeColor = () => {
this.setState({favoritecolor: "blue"});
}
render() {
return (
<div>
<h1>My Favorite Color is {this.state.favoritecolor}</h1>
<button type="button" onClick={this.changeColor}>Change color</button>
</div>
);
}
}
ReactDOM.render(<Header />, document.getElementById('root'));
render
The render()
method is of course called when a component gets updated,
it has to re-render the HTML to the DOM, with the new changes.
The example below has a button that changes the favorite color to blue:
Example:
Click the button to make a change in the component's state:
class Header extends React.Component {
constructor(props) {
super(props);
this.state = {favoritecolor: "red"};
}
changeColor = () => {
this.setState({favoritecolor: "blue"});
}
render() {
return (
<div>
<h1>My Favorite Color is {this.state.favoritecolor}</h1>
<button type="button" onClick={this.changeColor}>Change color</button>
</div>
);
}
}
ReactDOM.render(<Header />, document.getElementById('root'));
getSnapshotBeforeUpdate
In the getSnapshotBeforeUpdate()
method
you have access to the props
and
state
before the update, meaning that
even after the update, you can check what the values were before the
update.
If the getSnapshotBeforeUpdate()
method
is present, you should also include the
componentDidUpdate()
method, otherwise you will get an error.
The example below might seem complicated, but all it does is this:
When the component is mounting it is rendered with the favorite color "red".
When the component has been mounted, a timer changes the state, and after one second, the favorite color becomes "yellow".
This action triggers the update phase, and since this component has a
getSnapshotBeforeUpdate()
method, this method is executed, and writes a
message to the empty DIV1 element.
Then the componentDidUpdate()
method is
executed and writes a message in the empty DIV2 element:
Example:
Use the
getSnapshotBeforeUpdate()
method to find out
what the state
object looked like before
the update:
class Header extends React.Component {
constructor(props) {
super(props);
this.state = {favoritecolor: "red"};
}
componentDidMount() {
setTimeout(() => {
this.setState({favoritecolor: "yellow"})
}, 1000)
}
getSnapshotBeforeUpdate(prevProps, prevState) {
document.getElementById("div1").innerHTML =
"Before the update, the favorite was " + prevState.favoritecolor;
}
componentDidUpdate() {
document.getElementById("div2").innerHTML =
"The updated favorite is " + this.state.favoritecolor;
}
render() {
return (
<div>
<h1>My Favorite Color is {this.state.favoritecolor}</h1>
<div id="div1"></div>
<div id="div2"></div>
</div>
);
}
}
ReactDOM.render(<Header />, document.getElementById('root'));
componentDidUpdate
The componentDidUpdate
method
is called after the component is updated in the DOM.
The example below might seem complicated, but all it does is this:
When the component is mounting it is rendered with the favorite color "red".
When the component has been mounted, a timer changes the state, and the color becomes "yellow".
This action triggers the update phase, and since this component has
a componentDidUpdate
method, this method is
executed and writes a message in the empty DIV element:
Example:
The componentDidUpdate
method is called
after the update has been rendered in the DOM:
class Header extends React.Component {
constructor(props) {
super(props);
this.state = {favoritecolor: "red"};
}
componentDidMount() {
setTimeout(() => {
this.setState({favoritecolor: "yellow"})
}, 1000)
}
componentDidUpdate() {
document.getElementById("mydiv").innerHTML =
"The updated favorite is " + this.state.favoritecolor;
}
render() {
return (
<div>
<h1>My Favorite Color is {this.state.favoritecolor}</h1>
<div id="mydiv"></div>
</div>
);
}
}
ReactDOM.render(<Header />, document.getElementById('root'));
Unmounting
The next phase in the lifecycle is when a component is removed from the DOM, or unmounting as React likes to call it.
React has only one built-in method that gets called when a component is unmounted:
componentWillUnmount()
componentWillUnmount
The componentWillUnmount
method is
called when the component is about to be removed from the DOM.
Example:
Click the button to delete the header:
class Container extends React.Component {
constructor(props) {
super(props);
this.state = {show: true};
}
delHeader = () => {
this.setState({show: false});
}
render() {
let myheader;
if (this.state.show) {
myheader = <Child />;
};
return (
<div>
{myheader}
<button type="button" onClick={this.delHeader}>Delete Header</button>
</div>
);
}
}
class Child extends React.Component {
componentWillUnmount() {
alert("The component named Header is about to be unmounted.");
}
render() {
return (
<h1>Hello World!</h1>
);
}
}
ReactDOM.render(<Container />, document.getElementById('root'));