Skip to content

Data & State — Overview

React gives you a rich but fragmented menu: useState, useReducer, useContext, useRef, Redux, Zustand, react-query, SWR — each solving a different slice of the state problem. Svelte has a layered model that covers the same ground with fewer moving parts:

  • $state — component-local reactive state (replaces useState)
  • $derived — computed values that update automatically (replaces useMemo)
  • $effect — side effects that re-run on dependency change (replaces useEffect)
  • Svelte stores (writable, readable, derived) — shared state across components
  • .svelte.ts modules — module-level reactive state, imported like any JS module
  • {#await} — declarative async/loading/error state in markup

This module walks through each layer. Start here for a map, then dive into the dedicated lessons.

Component-local state: useState → $state

Section titled “Component-local state: useState → $state”
React
function Counter() {
const [count, setCount] = React.useState(0);
return (
<button onClick={() => setCount(c => c + 1)}>
Count: {count}
</button>
);
}
Svelte
<script>
let count = $state(0);
</script>
<button onclick={() => count++}>
Count: {count}
</button>

The key difference: $state variables are plain mutable variables. No setter, no functional update — just assign. Svelte’s compiler tracks reads and writes at the call site.

Which Svelte 5 rune directly replaces React's useState?
A Svelte writable store is most analogous to which React pattern?
How does Svelte's {#await} block differ from manually tracking loading state with useState?