💻
React - The Complete Guide (incl Hooks, React Rout
  • What you'll learn
  • Getting Started
    • Introduction
    • What is React
    • Learning Community
    • Components
    • Writing our First React Code
    • Why Should we Choose React
    • React Alternatives
    • SPA and MPA
      • Single Page Applications
      • Multi Page Applications
    • Course Outline
    • How to get the Most out of This Course
    • Useful Resources & Links
  • Two kinds of application
  • Hiều về let và const
  • Arrow Functions
  • Export và Import
  • Hiểu các tính năng cơ bản & Cú pháp
    • Module Introduction
    • The Build Workflow
    • Node.js Download
    • Using Create React App
    • Understanding the Folder Structure
    • Understanding Component Basics
    • Understanding JSX
    • JSX Restrictions
    • Creating a Functional Component
    • Components & JSX Cheat Sheet
    • Working with Components & Re-Using Them
    • Outputting Dynamic Content
    • Working with Props
    • Understanding the "children" Pro
    • Understanding & Using State
    • Props & State
    • Xử lý các sự kiện bằng các phương thức
    • Bạn có thể nghe những sự kiện nào?
    • Xử lý State
    • Đặt tên Function Components
    • Sử dụng useState() Hook cho xử lý State
    • Stateless vs Stateful Components
    • Truyền Method References giữa các Component
    • Liên kết hai chiều(Two-way Binding)
    • Thêm Styling với Stylesheets
    • Làm việc với Inline Styles
    • Tài nguyên & Liên kết hữu ích
Powered by GitBook
On this page

Was this helpful?

  1. Hiểu các tính năng cơ bản & Cú pháp

Truyền Method References giữa các Component

1. Làm cách nào để truyền một trình xử lý sự kiện (như onClick) cho một thành phần?

Truyền xử lý sự kiện và các chức năng khác như là props cho các component con:

<button onClick={this.handleClick}>

2. Làm thế nào để liên kết một chức năng với một thể hiện thành phần?

Có một số cách để đảm bảo các hàm có quyền truy cập vào các thuộc tính thành phần như this.propsvà this.state, tùy thuộc vào cú pháp và các bước xây dựng mà bạn đang sử dụng. Bind in Constructor (ES2015)

class Foo extends Component {
  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
  }
  handleClick() {
    console.log('Click happened');
  }
  render() {
    return <button onClick={this.handleClick}>Click Me</button>;
  }
}

Class Properties (Stage 3 Proposal)

class Foo extends Component {
  // Note: this syntax is experimental and not standardized yet.
  handleClick = () => {
    console.log('Click happened');
  }
  render() {
    return <button onClick={this.handleClick}>Click Me</button>;
  }
}

Bind in Render

class Foo extends Component {
  handleClick() {
    console.log('Click happened');
  }
  render() {
    return <button onClick={this.handleClick.bind(this)}>Click Me</button>;
  }
}

Arrow Function in Render

class Foo extends Component {
  handleClick() {
    console.log('Click happened');
  }
  render() {
    return <button onClick={() => this.handleClick()}>Click Me</button>;
  }
}

Sử dụng Arrow Function in Render sẽ tạo ra một Function mới mỗi lần thành phần render lại, có thể phá vỡ tối ưu hóa dựa trên so sánh nhận dạng nghiêm ngặt.

PreviousStateless vs Stateful ComponentsNextLiên kết hai chiều(Two-way Binding)

Last updated 4 years ago

Was this helpful?