प्रतिक्रिया वर्ग के घटक
रिएक्ट 16.8 से पहले, क्लास कंपोनेंट्स ही रिएक्ट कंपोनेंट की स्थिति और जीवनचक्र को ट्रैक करने का एकमात्र तरीका था। फ़ंक्शन घटकों को "राज्य-कम" माना जाता था।
हुक के साथ, फ़ंक्शन घटक अब लगभग क्लास घटकों के बराबर हैं। अंतर इतने छोटे हैं कि आपको शायद कभी भी रिएक्ट में क्लास कंपोनेंट का उपयोग करने की आवश्यकता नहीं होगी।
भले ही फंक्शन घटकों को प्राथमिकता दी जाती है, फिर भी रिएक्ट से क्लास कंपोनेंट्स को हटाने की कोई मौजूदा योजना नहीं है।
यह खंड आपको रिएक्ट में क्लास के घटकों का उपयोग करने का एक सिंहावलोकन देगा।
बेझिझक इस अनुभाग को छोड़ दें, और इसके बजाय फ़ंक्शन घटकों का उपयोग करें।
प्रतिक्रिया घटक
घटक कोड के स्वतंत्र और पुन: प्रयोज्य बिट हैं। वे जावास्क्रिप्ट फ़ंक्शन के समान उद्देश्य की पूर्ति करते हैं, लेकिन अलगाव में काम करते हैं और HTML को रेंडर () फ़ंक्शन के माध्यम से वापस करते हैं।
कंपोनेंट्स दो प्रकार के होते हैं, क्लास कंपोनेंट्स और फंक्शन कंपोनेंट्स, इस चैप्टर में आप क्लास कंपोनेंट्स के बारे में जानेंगे।
एक क्लास कंपोनेंट बनाएं
रिएक्ट कंपोनेंट बनाते समय, कंपोनेंट का नाम अपर केस लेटर से शुरू होना चाहिए।
घटक में extends React.Component
कथन शामिल होना चाहिए, यह कथन React.Component के लिए एक विरासत बनाता है, और आपके घटक को React.Component के कार्यों तक पहुंच प्रदान करता है।
घटक को एक render()
विधि की भी आवश्यकता होती है, यह विधि HTML लौटाती है।
उदाहरण
नामक एक वर्ग घटक बनाएँCar
class Car extends React.Component {
render() {
return <h2>Hi, I am a Car!</h2>;
}
}
अब आपके रिएक्ट एप्लिकेशन में कार नामक एक घटक है, जो एक
<h2>
तत्व देता है।
अपने एप्लिकेशन में इस घटक का उपयोग करने के लिए, सामान्य HTML के समान सिंटैक्स का उपयोग करें:
<Car />
उदाहरण
Car
घटक को "रूट" तत्व में प्रदर्शित करें :
ReactDOM.render(<Car />, document.getElementById('root'));
प्रमाणन हासिल करें!
$95 नामांकन
घटक निर्माता
यदि
आपके घटक में कोई फ़ंक्शन है, तो घटक शुरू होने पर इस फ़ंक्शन को कॉल किया जाएगा।constructor()
कंस्ट्रक्टर फ़ंक्शन वह जगह है जहाँ आप घटक के गुणों को आरंभ करते हैं।
रिएक्ट में, घटक गुणों को एक वस्तु में रखा जाना चाहिए जिसे
state
.
state
आप इस ट्यूटोरियल में बाद में इसके बारे में और जानेंगे ।
कंस्ट्रक्टर फ़ंक्शन वह भी है जहां आप स्टेटमेंट को शामिल करके पैरेंट कंपोनेंट की इनहेरिटेंस का सम्मान करते हैं super()
, जो पैरेंट कंपोनेंट के कंस्ट्रक्टर फ़ंक्शन को निष्पादित करता है, और आपके कंपोनेंट की पैरेंट कंपोनेंट ( React.Component
) के सभी फंक्शन्स तक पहुंच होती है।
उदाहरण
कार घटक में एक कंस्ट्रक्टर फ़ंक्शन बनाएं, और एक रंग गुण जोड़ें:
class Car extends React.Component {
constructor() {
super();
this.state = {color: "red"};
}
render() {
return <h2>I am a Car!</h2>;
}
}
रेंडर () फ़ंक्शन में रंग विशेषता का उपयोग करें:
उदाहरण
class Car extends React.Component {
constructor() {
super();
this.state = {color: "red"};
}
render() {
return <h2>I am a {this.state.color} Car!</h2>;
}
}
रंगमंच की सामग्री
घटक गुणों को संभालने का एक और तरीका है props
.
प्रॉप्स फ़ंक्शन तर्कों की तरह हैं, और आप उन्हें घटक में विशेषताओं के रूप में भेजते हैं।
props
आप अगले अध्याय में इसके बारे में और जानेंगे ।
उदाहरण
कार घटक को रंग पास करने के लिए एक विशेषता का उपयोग करें, और इसे रेंडर () फ़ंक्शन में उपयोग करें:
class Car extends React.Component {
render() {
return <h2>I am a {this.props.color} Car!</h2>;
}
}
ReactDOM.render(<Car color="red"/>, document.getElementById('root'));
कंस्ट्रक्टर में सहारा
यदि आपके कंपोनेंट में एक कंस्ट्रक्टर फ़ंक्शन है, तो प्रॉप्स को हमेशा कंस्ट्रक्टर को और रिएक्ट को भी पास किया जाना चाहिए। कंपोनेंट 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'));
घटकों में अवयव
हम अन्य घटकों के अंदर घटकों का उल्लेख कर सकते हैं:
उदाहरण
गैराज घटक के अंदर कार घटक का उपयोग करें:
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'));
फाइलों में अवयव
रिएक्ट कोड का पुन: उपयोग करने के बारे में है, और अपने कुछ घटकों को अलग-अलग फाइलों में सम्मिलित करना स्मार्ट हो सकता है।
ऐसा करने के लिए, फ़ाइल एक्सटेंशन के साथ एक नई फ़ाइल बनाएं .js
और उसके अंदर कोड डालें:
ध्यान दें कि फ़ाइल को रिएक्ट (पहले की तरह) आयात करके शुरू होना चाहिए, और इसे स्टेटमेंट के साथ समाप्त होना चाहिए 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'));
प्रतिक्रिया वर्ग घटक राज्य
रिएक्ट क्लास के घटकों में एक अंतर्निहित state
वस्तु होती है।
आपने देखा होगा कि हमने state
पहले कंपोनेंट कंस्ट्रक्टर सेक्शन में इस्तेमाल किया था।
ऑब्जेक्ट वह state
जगह है जहां आप घटक से संबंधित संपत्ति मूल्यों को संग्रहीत करते हैं।
जब state
वस्तु बदलती है, तो घटक पुन: प्रस्तुत करता है।
राज्य वस्तु बनाना
स्टेट ऑब्जेक्ट को कंस्ट्रक्टर में इनिशियलाइज़ किया जाता है:
उदाहरण
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
वस्तु का उपयोग करना
सिंटैक्स 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
वस्तु बदलना
राज्य वस्तु में मान बदलने के लिए, 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()
विधि का उपयोग करें, यह सुनिश्चित करेगा कि घटक जानता है कि यह अद्यतन किया गया है और रेंडर() विधि (और अन्य सभी जीवन चक्र विधियों) को कॉल करता है।
घटकों का जीवनचक्र
रिएक्ट में प्रत्येक घटक का एक जीवनचक्र होता है जिसे आप इसके तीन मुख्य चरणों के दौरान मॉनिटर और हेरफेर कर सकते हैं।
तीन चरण हैं: माउंटिंग , अपडेटिंग और अनमाउंटिंग ।
बढ़ते
माउंटिंग का अर्थ है तत्वों को DOM में डालना।
रिएक्ट में चार अंतर्निहित विधियाँ होती हैं, जिन्हें इस क्रम में, एक घटक को माउंट करते समय कहा जाता है:
constructor()
getDerivedStateFromProps()
render()
componentDidMount()
render()
विधि आवश्यक है और हमेशा कॉल की जाएगी, अन्य वैकल्पिक हैं और यदि आप उन्हें परिभाषित करते हैं तो उन्हें कॉल किया जाएगा ।
constructor
constructor()
विधि को किसी और चीज से पहले कहा जाता है, जब घटक शुरू किया जाता है, और यह प्रारंभिक और अन्य प्रारंभिक मूल्यों को स्थापित करने का प्राकृतिक स्थान है state
।
constructor()
विधि को तर्क के रूप में कहा जाता
है props
, और आपको हमेशा किसी और चीज से पहले कॉल करके शुरू करना चाहिए super(props)
, यह माता-पिता की कन्स्ट्रक्टर विधि शुरू करेगा और घटक को अपने माता-पिता ( React.Component
) से विधियों को प्राप्त करने की अनुमति देता है।
उदाहरण:
हर बार जब आप एक घटक बनाते हैं , constructor
तो विधि को प्रतिक्रिया द्वारा कहा जाता है:
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
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'));