Exploring Svelte to understand alternative frontend paradigms.
Project Day 7 - fokus pada eksplorasi paradigma frontend dengan Svelte.
Svelte Mini Dashboard adalah aplikasi web sederhana yang dibangun untuk memahami paradigma compile-time reactivity dari Svelte. Project ini bukan sekadar membuat UI, tapi lebih fokus pada eksplorasi konsep-konsep fundamental yang membedakan Svelte dari framework lain seperti React atau Vue.
Tujuan Utama:
Sebagai backend developer yang sedang eksplor frontend, saya memilih Svelte karena:
Svelte adalah compiler, bukan framework runtime. Saat build, Svelte mengubah component menjadi vanilla JavaScript yang highly optimized. Browser hanya load hasil compile, tanpa framework overhead.
// Yang kamu tulis (Svelte)
let count = 0;
$: doubled = count * 2;
// Yang di-compile jadi vanilla JS (simplified)
let count = 0;
let doubled = 0;
function update_count() {
doubled = count * 2;
update_dom();
}
Tidak perlu useState, setState, atau hooks yang kompleks:
<!-- Svelte -->
<script>
let count = 0;
$: doubled = count * 2; // Auto reactive!
</script>
<button on:click={() => count++}>
{count} (doubled: {doubled})
</button>
Bandingkan dengan React:
const [count, setCount] = useState(0);
const doubled = count * 2; // Re-compute every render
<button onClick={() => setCount(count + 1)}>
{count} (doubled: {doubled})
</button>
Svelte punya syntax yang lebih ringkas dan intuitif:
bind:value={text}Konsep: Reactive statement, conditional rendering
Konsep: Event dispatcher, lifecycle hooks, localStorage
Konsep: Reactive arrays, list rendering, two-way binding
| Technology | Version | Purpose |
|---|---|---|
| Svelte | 4.0+ | Frontend framework (compiler) |
| Vite | 5.0+ | Build tool & dev server |
| JavaScript | ES6+ | Programming language |
| CSS | 3 | Styling (scoped) |
Tidak menggunakan:
# Clone repository
git clone https://github.com/yourusername/svelte-mini-dashboard.git
cd svelte-mini-dashboard
# Install dependencies
npm install
# Run development server
npm run dev
# Build for production
npm run build
# Preview production build
npm run preview
Buka browser di http://localhost:5173
svelte-mini-dashboard/
โโโ public/ # Static assets
โโโ src/
โ โโโ components/
โ โ โโโ Counter.svelte # Counter component
โ โ โโโ ThemeToggle.svelte # Dark mode toggle
โ โ โโโ CardList.svelte # Dynamic card list
โ โโโ App.svelte # Root component
โ โโโ main.js # Entry point
โโโ index.html
โโโ package.json
โโโ vite.config.js
โโโ README.md
<script>
let count = 0;
$: doubled = count * 2; // Reactive statement
</script>
<button on:click={() => count++}>+</button>
Konsep:
$:{#if}<script>
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
function toggle() {
dispatch('themeChange', { isDark });
}
</script>
Konsep:
onMount)<script>
let cards = [...];
$: cardCount = cards.length; // Auto computed
</script>
{#each cards as card (card.id)}
<div>{card.title}</div>
{/each}
Konsep:
{#each}bind:valueDi Svelte, assignment biasa = reactive update:
let count = 0;
count = count + 1; // Auto update UI!
Di React, harus pakai setState:
const [count, setCount] = useState(0);
setCount(count + 1); // Manual state management
Kode yang otomatis re-run saat dependency berubah:
$: doubled = count * 2; // Re-run when count changes
$: if (count > 10) {
console.log('High count!');
}
$: {
// Multiple statements
localStorage.setItem('count', count);
document.title = `Count: ${count}`;
}
React equivalent: useEffect dengan dependency array.
<!-- Svelte: 1 line -->
<input bind:value={name} />
<!-- React: 3 lines -->
<input
value={name}
onChange={(e) => setName(e.target.value)}
/>
CSS otomatis scoped ke component tanpa CSS-in-JS:
<style>
button {
background: blue; /* Only affects this component */
}
</style>
Svelte compile component jadi kode yang:
Flow:
<!-- Inline -->
<button on:click={() => count++}>+</button>
<!-- Function -->
<button on:click={handleClick}>+</button>
<!-- Event modifiers -->
<button on:click|preventDefault={handleClick}>Submit</button>
| Aspek | Svelte | React |
|---|---|---|
| Paradigm | Compiler (compile-time) | Library (runtime) |
| Reactivity | Assignment biasa (count++) |
useState, setState |
| Bundle Size | Kecil (~5-10KB) | Besar (~40KB + deps) |
| Learning Curve | Mudah (less abstraction) | Sedang (hooks, closure) |
| Performance | Surgical DOM updates | Virtual DOM diffing |
| State Management | Built-in stores | External (Redux, Zustand) |
| Styling | Scoped CSS bawaan | Butuh library (styled-components) |
| Ecosystem | Kecil tapi growing | Sangat besar |
| Job Market | Emerging | Dominan |
| Use Case | Small-medium apps | Enterprise apps |
Svelte (Compile-Time):
Write Component โ Svelte Compiler โ Optimized JS โ Browser
(Build time) (No framework overhead)
React (Runtime):
Write Component โ Browser loads React โ Runtime reconciliation
(~40KB library) (Virtual DOM diffing)
Analogi:
Natural Reactivity
DX (Developer Experience)
Performance Bawaan
useMemo, useCallback, React.memoSimplicity
Ecosystem Lebih Kecil
Job Market
Complex State
Kapan Pakai Svelte:
Kapan Pakai React:
Svelte adalah game changer untuk developer yang:
Quote yang cocok:
"Svelte makes you feel like you're writing vanilla JavaScript, but with superpowers."
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ Svelte Mini Dashboard โ
โ Exploring compile-time reactivity โ
โ โ๏ธ Light Mode โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ Counter โ โ ๐ด Card List โ
โ โ โ โ
โ 42 โ โ Total Cards: 5 โ
โ Double: 84 โ โ โ
โ โ โ [Add form] โ
โ [ โ ] [Reset] โ โ โ
โ [ + ] โ โ โก Reactive โ
โ โ โ ๐จ Compiled โ
โ โ ๏ธ Count is high!โ โ ๐ฏ Minimal โ
โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ Svelte Mini Dashboard โ
โ Exploring compile-time reactivity โ
โ ๐ Dark Mode โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
[Dark theme with #1a1a1a background]
Contributions are welcome! Project ini adalah learning project, jadi feel free untuk:
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)Beberapa ide improvement yang bisa dikontribusikan:
MIT License - feel free to use this project for learning!
Made with โค๏ธ by [Yogiexc]
Day 7/7 - Fullstack Roadmap Challenge
โญ Star this repo if you found it helpful!
๐ฆ Share your learnings on Twitter with #SvelteLearning