💻
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

Sử dụng useState() Hook cho xử lý State

Khởi tạo State

useState cho phép chúng ta khai báo local state trong Function Component cách mà trước để chỉ dùng cho Class Component.

const [state, setState] = useState(initStateValue)
  • state: định nghĩa tên của state nó có thể là đơn giá trị hoặc object,.. (là tham số của useState)

  • setState: định nghĩa tên function dùng cho việc update state (là tham số của useState)

  • initialStateValue: là giá trị ban đầu của state. Ví dụ:

const Project=() => {
  const [count, setCount] = useState(0)
  const handleClick = () => setCount(age + 1)

  return (
    <div>
      Current count {count}.
      <div>
        <button onClick={handleClick}>Increment Count!</button>
      </div>
    </div>
  )
}
  • Như ví dụ thì bạn thấy ban đầu bạn khởi tạo state có tên là count với giá trị ban đầu là 0. Thì count ở đây là state name và là tham số đầu tiên của hàm useState và tham số thứ 2 sẽ hàm function setCount hàm xử lý khi mỗi lần ta nhấn click thì giá trị của state sẽ được tăng lên một. Công việc xử lý tăng lên 1 nó tương đương với hàm setState trong Class Components.

  • Vậy đó là cách khai báo cũng như cách update một state trong Function Component vậy nếu 1 Function Component có nhiều state thì sao?

Khai bao nhiều state cho Function Components

  • Thực ra khá đơn giản ta chỉ cần thêm N hàm useSate thôi.

const [state1, setState1] = useState(initialStateValue1)
const [state2, setState2] = useState(initialStateValue2)
PreviousĐặt tên Function ComponentsNextStateless vs Stateful Components

Last updated 4 years ago

Was this helpful?