What is the main difference between props and state?
4 phương ánCó giải thích
Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Đáp án đúng: "Props do not change after the initial render, while state can change over time.". Props là dữ liệu component cha truyền xuống, component con chỉ đọc chứ không tự sửa; state là dữ liệu nội bộ do chính component quản lý và thay đổi qua setState hoặc hàm set của useState, mỗi lần đổi thì component render lại. Props vẫn có thể nhận giá trị mới khi cha render lại, nhưng đối với component con chúng là bất biến (read-only), nên phát biểu này nắm đúng ý chính của sự khác biệt. "Props are mutable, while state is immutable." đảo ngược: props mới là chỉ đọc, còn state được cập nhật. "State is used for passing data between components, while props are used to manage component behavior." cũng đảo vai, vì truyền dữ liệu giữa các component là việc của props. "State is used for rendering UI, while props are used for functionality." sai vì cả props lẫn state đều được dùng để render giao diện; khác biệt nằm ở ai sở hữu dữ liệu và ai được thay đổi nó.
Cùng phần 4
How can you ensure that a prop is passed as an array with at least one element?Có lời giảiCâu hỏiHow can you pass parameters to an event handler in React?Có lời giảiCâu hỏiWhich of the following is used to pass a function from a parent component to a child component?Có lời giảiCâu hỏiCả 40 câu của chủ đề40 câu