All jobs / Frontend / web developer / Vue

Frontend / web developer interview question · 3–5 years

A useWindowSize composable adds a resize listener when it is called. After an hour of navigating, memory and handler counts keep growing.

What is wrong with the composable, and what is the contract a composable should follow?

The strongest answer

It never removes the listener. Register in onMounted and remove in onUnmounted inside the composable, call it synchronously in setup so those hooks bind to the component, and return refs

Why it works

Exactly — a composable owns its side effects: set up and tear down inside, tied to the caller’s lifecycle, state handed back as refs.

What a good answer covers

Answers that fall short

Who gets asked this

More Vue questions