Nâng CaoReact iconReact

shouldComponentUpdate hoạt động như thế nào và khi nào nên dùng?

shouldComponentUpdate(nextProps, nextState) trả về boolean, quyết định component có re-render hay không.

  • Mặc định return true.
  • Dùng để tối ưu performance bằng cách skip render khi props/state không thực sự thay đổi.
  • React.PureComponent tự động implement với shallow comparison.
  • Với function components, dùng React.memo thay thế.

Xem toàn bộ React cùng filter theo level & chủ đề con.

Mở danh sách React