Complete the code to add a click handler inline that shows an alert.
function Button() {
return <button onClick=[1]>Click me</button>;
}The onClick attribute expects a function. Using an inline arrow function like () => alert('Clicked!') works correctly.
Complete the code to define a separate function handler and use it in the button.
function Button() {
function handleClick() {
alert('Clicked!');
}
return <button onClick=[1]>Click me</button>;
}We pass the function handleClick itself to onClick, not calling it immediately.
Fix the error in the code by choosing the correct way to pass the handler.
function Button() {
const handleClick = () => alert('Clicked!');
return <button onClick=[1]>Click me</button>;
}The handler should be passed as a function reference, not called immediately.
Fill both blanks to create a button that calls a named function handler on click.
function Button() {
function [1]() {
alert('Clicked!');
}
return <button onClick=[2]>Click me</button>;
}The function name and the handler passed to onClick must match and be passed without parentheses.
Fill all three blanks to create a button that calls a function handler with a parameter on click.
function Button() {
function [1](message) {
alert(message);
}
return <button onClick={() => [2]([3])}>Click me</button>;
}The function name is showAlert. The inline arrow calls showAlert with the message 'Clicked!'.