React Class Components


ก่อน React 16.8 ส่วนประกอบ Class เป็นวิธีเดียวในการติดตามสถานะและวงจรชีวิตบนส่วนประกอบ React ส่วนประกอบของฟังก์ชันถือเป็น "ไร้สถานะ"

ด้วยการเพิ่ม Hooks ส่วนประกอบ Function เกือบจะเทียบเท่ากับส่วนประกอบ Class ความแตกต่างนั้นเล็กน้อยมากจนคุณอาจไม่จำเป็นต้องใช้องค์ประกอบ Class ใน React

แม้ว่าคอมโพเนนต์ของฟังก์ชันจะเป็นที่ต้องการ แต่ก็ยังไม่มีแผนที่จะลบคอมโพเนนต์ของ Class ออกจาก React ในปัจจุบัน

ส่วนนี้จะให้ภาพรวมเกี่ยวกับวิธีการใช้ส่วนประกอบ Class ใน React

ข้ามส่วนนี้ไปได้เลย และใช้ Function Components แทน


ส่วนประกอบปฏิกิริยา

ส่วนประกอบเป็นบิตโค้ดอิสระและนำกลับมาใช้ใหม่ได้ มีจุดประสงค์เดียวกับฟังก์ชัน JavaScript แต่ทำงานแยกกันและส่งกลับ HTML ผ่านฟังก์ชัน render()

ส่วนประกอบมีสองประเภท ได้แก่ ส่วนประกอบของคลาสและส่วนประกอบฟังก์ชัน ในบทนี้ คุณจะได้เรียนรู้เกี่ยวกับส่วนประกอบของคลาส


สร้างส่วนประกอบของคลาส

เมื่อสร้างส่วนประกอบ React ชื่อของส่วนประกอบต้องขึ้นต้นด้วยตัวพิมพ์ใหญ่

คอมโพเนนต์ต้องรวมextends React.Componentคำสั่ง คำสั่งนี้สร้างการสืบทอดไปยัง React.Component และให้คอมโพเนนต์ของคุณเข้าถึงฟังก์ชันของ React.Component

องค์ประกอบยังต้องการrender()เมธอด เมธอดนี้ส่งคืน HTML

ตัวอย่าง

สร้างองค์ประกอบคลาสที่เรียกว่าCar

class Car extends React.Component {
  render() {
    return <h2>Hi, I am a Car!</h2>;
  }
}

ตอนนี้แอปพลิเคชัน React ของคุณมีส่วนประกอบที่เรียกว่า Car ซึ่งส่งคืน <h2>องค์ประกอบ

ในการใช้องค์ประกอบนี้ในแอปพลิเคชันของคุณ ให้ใช้ไวยากรณ์ที่คล้ายกันเป็น HTML ปกติ: <Car />

ตัวอย่าง

แสดงCarส่วนประกอบในองค์ประกอบ "รูท":

ReactDOM.render(<Car />, document.getElementById('root'));


w3schools CERTIFIED . 2022

ได้รับการรับรอง!

ทำโมดูล React ทำแบบฝึกหัด ทำข้อสอบ และได้รับการรับรองจาก w3schools!!

$95 ลงทะเบียน

ตัวสร้างส่วนประกอบ

หากมีconstructor()ฟังก์ชันในคอมโพเนนต์ของคุณ ฟังก์ชันนี้จะถูกเรียกเมื่อคอมโพเนนต์เริ่มทำงาน

ฟังก์ชันคอนสตรัคเตอร์เป็นที่ที่คุณเริ่มต้นคุณสมบัติของคอมโพเนนต์

ใน React คุณสมบัติองค์ประกอบควรเก็บไว้ในวัตถุที่เรียกว่า state.

คุณจะได้เรียนรู้เพิ่มเติมstateในภายหลังในบทช่วยสอนนี้

ฟังก์ชันคอนสตรัคเตอร์ยังเป็นที่ที่คุณให้เกียรติการสืบทอดของคอมโพเนนต์พาเรนต์ด้วยการรวมsuper() คำสั่ง ซึ่งรันฟังก์ชันคอนสตรัคเตอร์ของคอมโพเนนต์พาเรนต์ และคอมโพเนนต์ของคุณมีสิทธิ์เข้าถึงฟังก์ชันทั้งหมดของคอมโพเนนต์พาReact.Componentเรนต์ ( )

ตัวอย่าง

สร้างฟังก์ชันตัวสร้างในส่วนประกอบรถยนต์ และเพิ่มคุณสมบัติสี:

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บทต่อไป

ตัวอย่าง

ใช้แอตทริบิวต์เพื่อส่งสีไปยังส่วนประกอบ Car และใช้ในฟังก์ชัน 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 ควรส่ง props ไปยัง Constructor และ 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'));


ส่วนประกอบในส่วนประกอบ

เราสามารถอ้างถึงส่วนประกอบภายในส่วนประกอบอื่น ๆ ได้:

ตัวอย่าง

ใช้ส่วนประกอบรถยนต์ภายในส่วนประกอบโรงรถ:

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'));


ส่วนประกอบในไฟล์

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'));


React Class ส่วนประกอบ State

ส่วนประกอบ React Class มี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()เมธอดเพื่อเปลี่ยนสถานะอ็อบเจ็กต์เสมอ เพื่อให้แน่ใจว่าคอมโพเนนต์รู้ว่าได้รับการอัพเดตและเรียกใช้เมธอด render() (และเมธอดวงจรชีวิตอื่นๆ ทั้งหมด)


วงจรชีวิตของส่วนประกอบ

แต่ละองค์ประกอบใน React มีวงจรชีวิตซึ่งคุณสามารถตรวจสอบและจัดการได้ในระหว่างสามขั้นตอนหลัก

สามขั้นตอนคือ: การ เมานท์ การอัปเดตและ การเลิกเมานท์


การติดตั้ง

การติดตั้งหมายถึงการวางองค์ประกอบลงใน DOM

React มีเมธอดในตัวสี่วิธีที่ได้รับการเรียกตามลำดับนี้เมื่อทำการติดตั้งส่วนประกอบ:

  1. constructor()
  2. getDerivedStateFromProps()
  3. render()
  4. componentDidMount()

จำเป็นต้อง ใช้render()เมธอดและจะถูกเรียกเสมอ ส่วนอื่นๆ เป็นทางเลือกและจะถูกเรียกหากคุณกำหนด


constructor

เมธอด นี้constructor()ถูกเรียกก่อนสิ่งอื่น เมื่อคอมโพเนนต์เริ่มต้น และเป็นสถานที่ธรรมชาติในการตั้งค่าเริ่มต้นstateและค่าเริ่มต้นอื่นๆ

constructor()เมธอดนี้ถูกเรียกด้วย props, เป็นอาร์กิวเมนต์ และคุณควรเริ่มต้นด้วยการเรียกใช้เมธอดsuper(props)ก่อนสิ่งอื่น ซึ่งจะเริ่มต้นเมธอด constructor ของพาเรนต์ และอนุญาตให้คอมโพเนนต์รับเมธอดจากพาReact.Componentเรนต์ ( )

ตัวอย่าง:

วิธี การ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

วิธี การgetDerivedStateFromProps()นี้ถูกเรียกก่อนที่จะแสดงผลองค์ประกอบใน DOM

นี่คือสถานที่ตามธรรมชาติในการตั้งค่า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:

  1. getDerivedStateFromProps()
  2. shouldComponentUpdate()
  3. render()
  4. getSnapshotBeforeUpdate()
  5. 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'));