Trung BìnhReact iconReact

Key prop dùng để reset component state như thế nào?

Thay đổi key của component khiến React unmount component cũ và mount component mới hoàn toàn, reset tất cả state.

  • Kỹ thuật hữu ích để: reset form sau submit <Form key={formKey} />, reset uncontrolled component khi data source thay đổi.
  • Đây là cách React có ý muốn để 'reset' component state từ bên ngoài.

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

Mở danh sách React