Fill in the blank in this event handling code to declare an inline handler that logs 'Clicked' on button click:
4 phương ánCó giải thích
<button onClick={
______
}
>Click</button>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: "() => console.log('Clicked')". Prop onClick cần nhận một HÀM để React gọi khi người dùng bấm; arrow function bọc lệnh log lại nên lệnh chỉ chạy lúc click. "console.log('Clicked')" là lời gọi hàm, chạy ngay khi component render nên chữ Clicked in ra lúc render, còn onClick nhận giá trị trả về undefined nên bấm nút không in gì. "event: console.log('Clicked')" không phải biểu thức hợp lệ, trong chỗ chèn biểu thức của JSX cú pháp nhãn kiểu này gây lỗi biên dịch. "handleClick = console.log('Clicked')" là phép gán: console.log vẫn chạy ngay lúc render, onClick nhận undefined, và gán vào biến chưa khai báo trong module ES (chế độ strict) còn ném ReferenceError.
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