ERP/.shared/ui-ux-pro-max/data/stacks/svelte.csv
2026-01-03 19:18:40 +08:00

11 KiB

1NoCategoryGuidelineDescriptionDoDon'tCode GoodCode BadSeverityDocs URL
21ReactivityUse $: for reactive statementsAutomatic dependency tracking$: for derived valuesManual recalculation$: doubled = count * 2let doubled; count && (doubled = count * 2)Mediumhttps://svelte.dev/docs/svelte-components#script-3-$-marks-a-statement-as-reactive
32ReactivityTrigger reactivity with assignmentSvelte tracks assignments not mutationsReassign arrays/objects to trigger updateMutate without reassignmentitems = [...items, newItem]items.push(newItem)Highhttps://svelte.dev/docs/svelte-components#script-2-assignments-are-reactive
43ReactivityUse $state in Svelte 5Runes for explicit reactivitylet count = $state(0)Implicit reactivity in Svelte 5let count = $state(0)let count = 0 (Svelte 5)Mediumhttps://svelte.dev/blog/runes
54ReactivityUse $derived for computed values$derived replaces $: in Svelte 5let doubled = $derived(count * 2)$: in Svelte 5let doubled = $derived(count * 2)$: doubled = count * 2 (Svelte 5)Medium
65ReactivityUse $effect for side effects$effect replaces $: side effectsUse $effect for subscriptions$: for side effects in Svelte 5$effect(() => console.log(count))$: console.log(count) (Svelte 5)Medium
76PropsExport let for propsDeclare props with export letexport let propNameProps without exportexport let count = 0let count = 0Highhttps://svelte.dev/docs/svelte-components#script-1-export-creates-a-component-prop
87PropsUse $props in Svelte 5$props rune for prop accesslet { name } = $props()export let in Svelte 5let { name, age = 0 } = $props()export let name; export let age = 0Medium
98PropsProvide default valuesDefault props with assignmentexport let count = 0Required props without defaultsexport let count = 0export let countLow
109PropsUse spread propsPass through unknown props{...$$restProps} on elementsManual prop forwarding<button {...$$restProps}><button class={$$props.class}>Lowhttps://svelte.dev/docs/basic-markup#attributes-and-props
1110BindingsUse bind: for two-way bindingSimplified input handlingbind:value for inputson:input with manual update<input bind:value={name}><input value={name} on:input={e => name = e.target.value}>Lowhttps://svelte.dev/docs/element-directives#bind-property
1211BindingsBind to DOM elementsReference DOM nodesbind:this for element referencequerySelector in onMount<div bind:this={el}>onMount(() => el = document.querySelector())Medium
1312BindingsUse bind:group for radios/checkboxesSimplified group handlingbind:group for radio/checkbox groupsManual checked handling<input type="radio" bind:group={selected}><input type="radio" checked={selected === value}>Low
1413EventsUse on: for event handlersEvent directive syntaxon:click={handler}addEventListener in onMount<button on:click={handleClick}>onMount(() => btn.addEventListener())Mediumhttps://svelte.dev/docs/element-directives#on-eventname
1514EventsForward events with on:eventPass events to parenton:click without handlercreateEventDispatcher for DOM events<button on:click>dispatch('click', event)Low
1615EventsUse createEventDispatcherCustom component eventsdispatch for custom eventson:event for custom eventsdispatch('save', { data })on:save without dispatchMediumhttps://svelte.dev/docs/svelte#createeventdispatcher
1716LifecycleUse onMount for initializationRun code after component mountsonMount for setup and data fetchingCode in script body for side effectsonMount(() => fetchData())fetchData() in script bodyHighhttps://svelte.dev/docs/svelte#onmount
1817LifecycleReturn cleanup from onMountAutomatic cleanup on destroyReturn function from onMountSeparate onDestroy for paired cleanuponMount(() => { sub(); return unsub })onMount(sub); onDestroy(unsub)Medium
1918LifecycleUse onDestroy sparinglyOnly when onMount cleanup not possibleonDestroy for non-mount cleanuponDestroy for mount-related cleanuponDestroy for store unsubscribeonDestroy(() => clearInterval(id))Low
2019LifecycleAvoid beforeUpdate/afterUpdateUsually not neededReactive statements insteadbeforeUpdate for derived state$: if (x) doSomething()beforeUpdate(() => doSomething())Low
2120StoresUse writable for mutable stateBasic reactive storewritable for shared mutable stateLocal variables for shared stateconst count = writable(0)let count = 0 in moduleMediumhttps://svelte.dev/docs/svelte-store#writable
2221StoresUse readable for read-only stateExternal data sourcesreadable for derived/external datawritable for read-only datareadable(0, set => interval(set))writable(0) for timerLowhttps://svelte.dev/docs/svelte-store#readable
2322StoresUse derived for computed storesCombine or transform storesderived for computed valuesManual subscription for derivedderived(count, $c => $c * 2)count.subscribe(c => doubled = c * 2)Mediumhttps://svelte.dev/docs/svelte-store#derived
2423StoresUse $ prefix for auto-subscriptionAutomatic subscribe/unsubscribe$storeName in componentsManual subscription{$count}count.subscribe(c => value = c)High
2524StoresClean up custom subscriptionsUnsubscribe when component destroysReturn unsubscribe from onMountLeave subscriptions openonMount(() => store.subscribe(fn))store.subscribe(fn) in scriptHigh
2625SlotsUse slots for compositionContent projection<slot> for flexible contentProps for all content<slot>Default</slot><Component content="text"/>Mediumhttps://svelte.dev/docs/special-elements#slot
2726SlotsName slots for multiple areasMultiple content areas<slot name="header">Single slot for complex layouts<slot name="header"><slot name="footer"><slot> with complex conditionalsLow
2827SlotsCheck slot content with $$slotsConditional slot rendering$$slots.name for conditional renderingAlways render slot wrapper{#if $$slots.footer}<slot name="footer"/>{/if}<div><slot name="footer"/></div>Low
2928StylingUse scoped styles by defaultStyles scoped to component<style> for component stylesGlobal styles for component:global() only when needed<style> all globalMediumhttps://svelte.dev/docs/svelte-components#style
3029StylingUse :global() sparinglyEscape scoping when needed:global for third-party stylingGlobal for all styles:global(.external-lib)<style> without scopingMedium
3130StylingUse CSS variables for themingDynamic stylingCSS custom propertiesInline styles for themesstyle="--color: {color}"style="color: {color}"Low
3231TransitionsUse built-in transitionsSvelte transition directivestransition:fade for simple effectsManual CSS transitions<div transition:fade><div class:fade={visible}>Lowhttps://svelte.dev/docs/element-directives#transition-fn
3332TransitionsUse in: and out: separatelyDifferent enter/exit animationsin:fly out:fade for asymmetricSame transition for both<div in:fly out:fade><div transition:fly>Low
3433TransitionsAdd local modifierPrevent ancestor triggertransition:fade|localGlobal transitions for lists<div transition:slide|local><div transition:slide>Medium
3534ActionsUse actions for DOM behaviorReusable DOM logicuse:action for DOM enhancementsonMount for each usage<div use:clickOutside>onMount(() => setupClickOutside(el))Mediumhttps://svelte.dev/docs/element-directives#use-action
3635ActionsReturn update and destroyLifecycle methods for actionsReturn { update, destroy }Only initial setupreturn { update(params) {}, destroy() {} }return destroy onlyMedium
3736ActionsPass parameters to actionsConfigure action behavioruse:action={params}Hardcoded action behavior<div use:tooltip={options}><div use:tooltip>Low
3837LogicUse {#if} for conditionalsTemplate conditionals{#if} {:else if} {:else}Ternary in expressions{#if cond}...{:else}...{/if}{cond ? a : b} for complexLowhttps://svelte.dev/docs/logic-blocks#if
3938LogicUse {#each} for listsList rendering{#each} with keyMap in expression{#each items as item (item.id)}{items.map(i => `<div>${i}</div>`)}Medium
4039LogicAlways use keys in {#each}Proper list reconciliation(item.id) for unique keyIndex as key or no key{#each items as item (item.id)}{#each items as item, i (i)}High
4140LogicUse {#await} for promisesHandle async states{#await} for loading/error statesManual promise handling{#await promise}...{:then}...{:catch}{#if loading}...{#if error}Mediumhttps://svelte.dev/docs/logic-blocks#await
4241SvelteKitUse +page.svelte for routesFile-based routing+page.svelte for route componentsCustom routing setuproutes/about/+page.svelteroutes/About.svelteMediumhttps://kit.svelte.dev/docs/routing
4342SvelteKitUse +page.js for data loadingLoad data before renderload function in +page.jsonMount for data fetchingexport function load() {}onMount(() => fetchData())Highhttps://kit.svelte.dev/docs/load
4443SvelteKitUse +page.server.js for server-onlyServer-side data loading+page.server.js for sensitive data+page.js for API keys+page.server.js with DB access+page.js with DB accessHigh
4544SvelteKitUse form actionsServer-side form handling+page.server.js actionsAPI routes for formsexport const actions = { default }fetch('/api/submit')Mediumhttps://kit.svelte.dev/docs/form-actions
4645SvelteKitUse $app/stores for app state$page $navigating $updated$page for current page dataManual URL parsingimport { page } from '$app/stores'window.location.pathnameMediumhttps://kit.svelte.dev/docs/modules#$app-stores
4746PerformanceUse {#key} for forced re-renderReset component state{#key id} for fresh instanceManual destroy/create{#key item.id}<Component/>{/key}on:change={() => component = null}Lowhttps://svelte.dev/docs/logic-blocks#key
4847PerformanceAvoid unnecessary reactivityNot everything needs $:$: only for side effects$: for simple assignments$: if (x) console.log(x)$: y = x (when y = x works)Low
4948PerformanceUse immutable compiler optionSkip equality checksimmutable: true for large listsDefault for all components<svelte:options immutable/>Default without immutableLow
5049TypeScriptUse lang="ts" in scriptTypeScript support<script lang="ts">JavaScript for typed projects<script lang="ts"><script> with JSDocMediumhttps://svelte.dev/docs/typescript
5150TypeScriptType props with interfaceExplicit prop typesinterface $$Props for typesUntyped propsinterface $$Props { name: string }export let nameMedium
5251TypeScriptType events with createEventDispatcherType-safe eventscreateEventDispatcher<Events>()Untyped dispatchcreateEventDispatcher<{ save: Data }>()createEventDispatcher()Medium
5352AccessibilityUse semantic elementsProper HTML in templatesbutton nav main appropriatelydiv for everything<button on:click><div on:click>High
5453AccessibilityAdd aria to dynamic contentAccessible state changesaria-live for updatesSilent dynamic updates<div aria-live="polite">{message}</div><div>{message}</div>Medium