Assess the impact of using inline event handlers in React components. Which statement is true?
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: "Inline event handlers can lead to performance issues due to the creation of new functions on each render". Viết một hàm mũi tên ngay trong prop onClick nghĩa là mỗi lần component render, một hàm mới với tham chiếu mới được tạo ra. Bình thường chi phí này rất nhỏ, nhưng khi truyền hàm đó xuống component con bọc React.memo hay dùng làm phụ thuộc của useEffect, tham chiếu đổi mỗi lần sẽ phá ghi nhớ và gây render lại thừa; useCallback là cách giữ tham chiếu ổn định. Vì vậy phát biểu đúng là nó có thể gây vấn đề hiệu năng, chứ không phải luôn luôn. "Inline event handlers improve performance by reducing the number of function calls" sai vì viết inline không giảm lời gọi hàm mà còn tạo thêm hàm. "Inline event handlers are the only way to handle events in React" sai vì có thể truyền một hàm đặt tên như onClick={handleClick}. "Inline event handlers are more secure than named event handlers" sai vì hai cách chỉ khác cú pháp, không khác gì về bảo mật.
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