Skip to content
JournalTORIS / FIELD NOTES

← Blog Project notes

When the Page Changed, the Click Wiring Had to Change Too

A look back at useClickable and page transitions in educational content, covering event registration and cleanup, stale state references, and boundaries when handling DOM outside React.

3 min read Read the Korean original

Joohwan Yu · Founder, Full Stack Developer

Toris · Published

Contents

The educational content had a feature where clicking a specific element inside an explanation triggered the next action. While finding clickable elements and attaching events with a hook like useClickable, I found that the wiring also had to be refreshed whenever the page changed. It worked differently from a button that is fully handled declaratively inside a React component.

The Screen and the Events Didn’t Change at the Same Speed

When the explanation content changes, the DOM changes too. If you leave events attached to the old elements as they are, the new elements may not respond. Conversely, if you only register on every update, you can no longer explain the lifetime of the existing connections. Even when what’s on screen is correct, click behavior can still reference old state.

What mattered in this work was not a single useState to store whether something was clicked. It was which content version the events had been attached for, and when the connection should be cut. I also had to distinguish whether to go by the page number alone or by the actual replacement of the content.

The Review Order I Use Now

First, I would check whether the event target is an element React creates directly or one created by external content. In the former case, revealing the intent with onClick is the natural choice. In the latter, I would see whether events can be delegated from a stable parent element. Delegation does need its own rules, though: which target to find when a nested element is clicked, and whether to run only permitted actions.

If you have to register directly, the function used at registration and the function used at removal must be the same. Here I would not carelessly fold in the problem of reading the latest state. Separating the lifetime of the event connection from the lifetime of the values the callback reads makes it easier to explain why the dependency array was written the way it was.

Verification Had to Go Back and Forth Too

Rather than clicking once on the first page, going from page A to B and back to A is the more important check. That lets you confirm how many times the same click gets handled, and whether behavior for content no longer on screen is still lingering. At the time I fixed things by asking about these boundaries, and I’m left with some regret that I couldn’t automate every case.

From this experience, I came to see an event listener not as a simple function connection but as a resource that needs cleaning up. If you can’t explain the removal condition right next to the registration code, bugs get harder to find as pages multiply.

Technical reference: Synchronizing with Effects in React (external systems)

Advertisement