Urgent.News

What's breaking now, across thousands of outlets.

Tech

Vue Composables: The Shared State Trap (+ Cheat Sheet)

You build a useCounter() composable, drop <Counter /> on the page twice, and click the first button. Both counters go up. You didn't copy-paste a bug. You wrote twelve lines of completely ordinary-looking Composition API code, and Vue executed every one of them correctly. The surprise isn't a Vue bug — it's a gap in your mental model of what a composable actually is , and that gap is exactly…

The issue arises when two instances of a composable, such as `useCounter()`, are used on the same page and both counters share the same state. This occurs due to the default behavior of JavaScript's scoping rules, where state declared inside the function is created fresh every time the function runs, resulting in private state for each instance.

However, state declared outside the function, at the module scope, is created only once, the first time anything imports the module, and subsequent calls to `useCounter()` return a reference to that same ref. This can lead to confusion and unexpected behavior, as demonstrated by the shared state of both counters in this scenario.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

The Dropdown You've Been Faking for a Decade

Design wants a country picker. Nothing fancy — a dropdown, a small flag icon next to each name. You reach for <select> , type the first <option> , and hit the wall every frontend dev hits eventually…

I built a browser extension to show pixel diffs in GitHub PRs

I've been using AI to write more code, which has also meant writing more tests—including screenshot tests to catch unexpected UI changes.

  • Software developer created gh-pixel-diff extension for GitHub PRs.
  • Extension adds pixel diff functionality within PRs without installation.
  • Users can save comparison views as PNG files for review comments.

More from Tuesday 22 September →