Reactive declarations use the $: syntax to automatically recompute values when dependencies change. Example: $: doubled = count * 2. They run whenever any referenced values change.
Derived values can be created using reactive declarations ($:) or derived stores. They automatically update when dependencies change. Used for computed values that depend on other state.
Async derived stores handle asynchronous transformations. Use derived with set callback. Handle loading states. Support cancellation. Manage async dependencies.
Custom stores implement subscribe function. Can add custom methods. Handle internal state. Support custom update logic. Implement cleanup on unsubscribe.
Store validation ensures data integrity. Implement validation rules. Handle validation errors. Support schema validation. Manage validation state.
Store compression reduces data size. Implement compression algorithms. Handle serialization. Support decompression. Manage compressed state.
Reactivity in Svelte is the automatic updating of the DOM when data changes. It's handled through assignments to declared variables and the $: syntax. Svelte's compiler creates the necessary code to update the DOM when dependencies change.
Stores can be subscribed to using subscribe() method or automatically in templates using $ prefix. Example: $store in template or store.subscribe(value => {}) in script.
Store subscriptions cleanup happens automatically with $ prefix. Manual cleanup needs unsubscribe call. Handle cleanup in onDestroy. Prevent memory leaks.
Store synchronization handles multiple instances. Implement cross-tab sync. Support real-time updates. Handle conflicts. Manage sync state.
Store testing verifies store behavior. Implement test utilities. Handle async testing. Support mocking. Manage test state.
Store documentation describes store usage. Implement documentation generation. Handle API documentation. Support examples. Manage documentation state.
Writable stores are created using writable() from svelte/store. Example: const count = writable(0). Provides methods like set() and update() to modify the store value. Subscribe to changes using subscribe() method.
Derived stores are created using derived() from svelte/store. Take one or more stores as input. Transform values using callback function. Update automatically when source stores change.
Store persistence uses localStorage or sessionStorage. Implement auto-save functionality. Handle serialization. Support state rehydration. Manage persistence errors.
Store composition combines multiple stores. Create higher-order stores. Handle dependencies between stores. Support store chaining. Manage composite updates.
Store optimization includes batching updates. Implement update debouncing. Handle performance bottlenecks. Support selective updates. Monitor update frequency.
Store encryption secures sensitive data. Implement encryption/decryption. Handle key management. Support secure storage. Manage encrypted state.
Store monitoring tracks store usage. Implement metrics collection. Handle performance tracking. Support debugging tools. Manage monitoring state.
Arrays must be updated using assignment for reactivity. Use methods like [...array, newItem] for additions, array.filter() for removals, or array.map() for updates. Assignment triggers reactivity.
Writable stores can be modified using set() and update(), while readable stores are read-only. Readable stores are created using readable() and only change through their start function.
Auto-subscription happens when using $ prefix with stores in components. Svelte automatically handles subscribe and unsubscribe. No manual cleanup needed. Works in template and reactive statements.
The set function directly sets a new value for a writable store. Example: count.set(10). Triggers store updates and notifies all subscribers. Used for direct value updates.
Objects must be updated through assignment for reactivity. Use spread operator or Object.assign for updates. Example: obj = {...obj, newProp: value}. Assignment triggers reactivity.
Store initialization can be synchronous or async. Handle initial values. Support lazy initialization. Manage loading states. Handle initialization errors.
Store error handling includes error states. Implement error recovery. Support error notifications. Handle async errors. Manage error boundaries.
Store middleware intercepts store operations. Implement custom behaviors. Handle side effects. Support middleware chain. Manage middleware order.
Store time-travel tracks state history. Implement undo/redo. Handle state snapshots. Support history compression. Manage memory usage.
Store migrations handle version changes. Implement data transforms. Support backward compatibility. Handle migration errors. Manage migration state.