A component tracks how many keys have been pressed and how many times a button has been clicked. Each keypress should add exactly 1 to the key count regardless of how many times the button has been clicked.
Press a key — the count increments normally. Click Re-render, then press keys again — the count jumps by more than 1 per keypress. The more times Re-render is clicked, the larger the jump per keypress becomes.
Every time the effect re-runs, it registers a new keydown listener. Without a cleanup function, the old listeners are never removed — so after two re-renders, three listeners all fire on every keypress.
Every time the effect re-runs, it registers a new keydown listener. Without a cleanup function, the old listeners are never removed — so after two re-renders, three listeners all fire on every keypress.