Svelte · Middle · Question 13 of 75
How do you write reusable reactive logic (like a hook) in Svelte 5?
// counter.svelte.js
export function createCounter() {
let count = $state(0);
return {
get count() { return count; },
increment: () => count++,
};
}- ✓Correct answer: A function in a
.svelte.jsfile that uses runes and returns getters (or a class with$statefields) - 2A store is the only way
- 3A component without markup
Explanation
Returning count directly would give a snapshot of the number; a getter keeps reads reactive.