Handling Events — Concept Check
Questions on Handling Events. Pass a function, do not call one. Most early event bugs are one pair of parentheses in the wrong place.
Questions in this quiz
7 questions on Handling Events — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have a button in a React component: `<button onClick={handleRun()}>Run</button>`. What issue might this cause?
- AThe function will never be called.
- BThe function runs on render, causing potential infinite re-renders.
- CThe button will not respond to clicks.
- DThe function will be called twice on each click.
2.A developer uses `<button onClick={() => onOpen(problem.slug)}>Open</button>`. Why is this correct?
- AIt calls the function immediately, which is correct.
- BIt ensures the function is never called.
- CIt passes a reference with an argument correctly.
- DIt avoids creating a new function on each render.
3.Why might adding a thousand elements with React event handlers not degrade performance significantly?
- AReact attaches one listener at the root.
- BReact uses native DOM events directly.
- CReact attaches one listener per element.
- DReact batches event listeners efficiently.
4.What happens if you add a document-level listener in a React app?
- AIt runs after React's handlers, ensuring proper cleanup.
- BIt runs before React's handlers, affecting 'click outside to close' logic.
- CIt conflicts with React's root listener, causing errors.
- DIt has no effect due to React's synthetic events.
5.A div with `onClick` is added: `<div onClick={select}>Select</div>`. What is a potential issue?
- AIt will not trigger the function.
- BIt is not keyboard accessible.
- CIt requires a wrapper function.
- DIt will trigger twice on click.
6.Why is using `onClick={handleRun()}` in a React component often a mistake?
- AIt passes a function reference correctly.
- BIt is necessary for passing arguments.
- CIt optimizes performance by pre-calling.
- DIt calls the function immediately, not on click.
7.Why should async handlers disable a button while in flight?
- ATo prevent double submissions.
- BTo ensure the function runs only once.
- CTo improve rendering speed.
- DTo avoid creating new functions.