• Stars
    star
    491
  • Rank 89,636 (Top 2 %)
  • Language
    JavaScript
  • Created about 4 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React hook examples ported to Svelte

React Hooks in Svelte

React Hook examples ported to Svelte.

New 📣: React ⇆ Svelte Cheatsheet

useState

In Svelte, const [varName, set] = useState(initialValue) becomes let varName = initialValue. The setter function is replaced with JavaScript's assignment operator =.

React example
Svelte example

Diff of useState

useEffect

In React, there are 3 ways to useEffect().

  1. With null dependencies: useEffect(fn). This runs on every render.
  2. With an empty array as dependencies: useEffect(fn, []). This runs during mount, and cleanup function runs on unmount.
  3. With a list of dependency vars: useEffect(fn, [a, b, c]). This reavaulates whenever a dependency changes. The cleanup runs whenever dependencies change and during unmount.

This is an example of #2, where the callback runs when component is mounted and cleanup runs when unmounted.

React example
Svelte example

Diff of useEffect

useMemo

React example
Svelte example

In Svelte, all reactive statements are memoized. Instead of const var = useMemo(() => expression, dependencies), you can use $: var = expression. Notice that with Svelte, you don't need to declare the dependencies. The compiler infers them for you.

Diff of useMemo

useRef

React example
Svelte example

In Svelte, useRef() is bind:this.

Diff of useRef

useReducer

React example
Svelte example

In Svelte, useReducer() can be replaced with a writable() store. Instead of dispatching using a switch statement, functions can be defined on the store directly.

Diff of useReducer

useCallback

In React, useCallback is used to memoize functions. This is needed because event handlers are re-defined on every render.

Take this example:

// This function (component) is executed on every render
function Component() {
  // this event handler is redefined on every render
  const handleClick = () => alert("hello")
  
  // because `handleClick` is redefined on every render, `ChildComponent` will be re-rendered too. Because its `onClick` prop is considered changed.
  return <ChildComponent onClick={handleClick}/>
}

So we need to wrap handleClick in a useCallback, to give a hint to the rendering system that the handler wasn't changed.

In Svelte this isn't needed, because event handlers are declared inside <script> tags. They aren't defined in the render path and therefore arent't redefined on every render. They are defined once per component, so they work similar to how event handlers worked with React.Component.

useContext

Context in both frameworks are very similar. One difference is that context in Svelte is not reactive by default. To make it reactive, context data should be wrapped in a store.

Another difference is that context in Svelte does not insert anything into the visual component tree. There is no <Context.Provider> element like in React, instead use the setContext() function.

React example
Svelte example

Root component Diff of useEffect App

Intermediate component Diff of useEffect Toolbar

Grand-child component Diff of useEffect ThemedButton

License

MIT

More Repositories

1

svelte-persisted-store

A Svelte store that persists to localStorage
TypeScript
963
star
2

design-patterns-in-elixir

Common design patterns applied in Elixir
Elixir
598
star
3

svelte-stripe

Everything you need to add Stripe Elements to your Svelte project
Svelte
409
star
4

commerce_billing

A payment processing library for Elixir
Elixir
176
star
5

xml_builder

Elixir library for generating XML
Elixir
163
star
6

micro-svelte-compiler

Micro Svelte compiler (naive clone)
JavaScript
113
star
7

airbadge

Svelte
87
star
8

phoenix-billing-demo

A demo of using commerce_billing with phoenix
Elixir
61
star
9

sveltekit-saas

JavaScript
59
star
10

angular-rails-api

A template for creating angular+rails apps
Ruby
53
star
11

elixir-ffi

Foreign Function Interface (FFI) for Elixir
Elixir
53
star
12

spree-faq

Frequently Asked Question extension for Spree
Ruby
46
star
13

svelte-bridge

Use your Svelte components from React & Vue.
JavaScript
42
star
14

sky-cart

Cloud-native headless shopping cart
JavaScript
36
star
15

vite-plugin-notifier

Get visual feedback that HMR completed
JavaScript
36
star
16

sveltekit-stripe-subscriptions

Example of Stripe Subscriptions & PaymentElement with SvelteKit
JavaScript
33
star
17

svelte-kit-ecommerce-3d

Demo using Threlte, SvelteKit and Stripe Checkout
Svelte
33
star
18

svelte-markdoc

Markdoc preprocessor for Svelte
JavaScript
33
star
19

supabase-active-record

ActiveRecord pattern for supabase.io
JavaScript
32
star
20

blip

A fault-tolerant and concurrent StatsD server
Elixir
30
star
21

blog-template

A markdown-style blog template for Sapper
JavaScript
28
star
22

xuber2

An example ridesharing app built with Elixir
Elixir
25
star
23

svelte-animation-store

JavaScript
24
star
24

hypersonic

A multi-player markdown editor and wiki.
Svelte
21
star
25

shiki-transformer-copy-button

A Shiki Transformer that adds a Copy button
JavaScript
20
star
26

svelte-http-store

A Svelte store backed by the fetch API
JavaScript
19
star
27

spree-html-email

HTML Formatted emails for spree
Ruby
18
star
28

spree-sitemap-generator

Spree Sitemap Generator
Ruby
17
star
29

glue

React hook for server-side state
JavaScript
17
star
30

tonys-apizza

POS for a pizza shop. Using SvelteKit, Prisma, and Stripe.
Svelte
17
star
31

sveltekit-reroute

JavaScript
17
star
32

bcm2835

Ruby bindings for libbcm2835
Ruby
17
star
33

spree-contact-form

Spree extension that provides a basic contact form
Ruby
14
star
34

vertex

Store multi-tenant metrics in ClickHouse
Elixir
14
star
35

ride_share

A clone of Uber/Lyft
Elixir
13
star
36

threlte-svg-extrusion

Svelte
12
star
37

svelte-kit-test-helpers

Testing utilities for SvelteKit
JavaScript
12
star
38

materialize-svelte

JavaScript
12
star
39

sprionic-demo

Demo Spree+Ionic Android/iOS App
JavaScript
12
star
40

threlte-starter

Starter template for SvelteKit + Threlte projects
Svelte
11
star
41

markdown-mail

Example CRUD app with SvelteKit + Prisma
JavaScript
11
star
42

1000experiments.dev

Svelte
11
star
43

auth-env

Load auth.js providers dynamically, by detecting environment variables
JavaScript
11
star
44

git-ssh-server

A small git/ssh server for hosting repos
JavaScript
10
star
45

phoenix_svelte_rollup_example

Elixir
10
star
46

svelte-codemirror

Svelte
9
star
47

combinatorics

Combinatoric utilities for Elixir
Elixir
9
star
48

airbadge-example

JavaScript
8
star
49

svelte-time-distance

Svelte component to display time distances in a human readable format.
JavaScript
8
star
50

transponder

Elixir
7
star
51

testing-with-sveltekit

JavaScript
7
star
52

spree-product-feed

A Spree extension that provides an RSS feed for products
Ruby
7
star
53

svelte-snowpack-template

A snowpack template for svelte with support for jest, eslint, postcss, tailwind, and rollup.
JavaScript
7
star
54

idp-experiment

JavaScript
6
star
55

httpx

A tiny concurrent & fault-tolerant HTTP server that uses pattern matching for routing
Elixir
6
star
56

nmea

Parser for NMEA compliant sensors
Elixir
6
star
57

svelte-preprocess-tailwind

Preprocess tailwind expressions with Svelte
JavaScript
5
star
58

spree_stock_notifications

Get notifications when stock is low or item goes out of stock
Ruby
5
star
59

rails-jquery-sortable

Example of a Rails app using jQuery Sortable plugin
Ruby
5
star
60

river

Example distributed key/value store using RAFT
Elixir
5
star
61

requel

Import SQL files with Vite
TypeScript
5
star
62

prisma-database-cleaner

Utility for cleaning Prisma databases between test runs
JavaScript
4
star
63

scrolly-snap-telling

Svelte
4
star
64

live_imu

Displays IMU data using Phoenix LiveView
Elixir
4
star
65

xuber-old

Demo of Phoenix Channels + GeoLocation API
Elixir
4
star
66

svelte-kit-pusher-example

Svelte
4
star
67

ecto_preloader

Preload your data efficiently with joins
Elixir
4
star
68

svelte-esm-loader-experimental

JavaScript
4
star
69

svelte-kit-with-docker

Run your SvelteKit dev environment with Docker
JavaScript
4
star
70

cloud-scaling-experiment

Shell
4
star
71

svelte-codemirror-test

Svelte
4
star
72

otp-talk

Elixir
4
star
73

spree-awesome-blog

Basic blog and management tools designed for Spree
JavaScript
4
star
74

guzzle

Multi-core streaming build system
Elixir
4
star
75

supabase-reactions

Svelte
4
star
76

math4devs.com

Svelte
4
star
77

svelte-chart

Svelte
3
star
78

elixir_vite_app

Elixir
3
star
79

name-panda

JavaScript
3
star
80

intel8085

Assembler, Emulator and Debugger for the Intel 8085A microprocessor
C++
3
star
81

sk-cookie-store-experiment

JavaScript
3
star
82

elixir_inventory_management_sample

Example inventory management system w/ Elixir
Elixir
3
star
83

open-props-mobile-menu

Svelte
3
star
84

unsearch

A set of adapters for working with common search engines
TypeScript
2
star
85

electron

2
star
86

rails-templates

Rails Application Templates
Ruby
2
star
87

eagle-ruby

Ruby
2
star
88

http-shark

An HTTP proxy & GUI for recording/debugging REST API calls.
CSS
2
star
89

covid-ca

CSS
2
star
90

faas

Elixir
2
star
91

gql_commerce

JavaScript
2
star
92

svelte-checkout

JavaScript
2
star
93

octobase

Hybrid relational/document database
Erlang
2
star
94

tiltd

A rack daemon for tilt
Ruby
2
star
95

http-firebase-proxy

Proxy HTTP requests, and record them in Firebase for debugging
JavaScript
2
star
96

osm-svelte-kit-experiment

Svelte
2
star
97

multi_edit

Elixir
2
star
98

sapper-data-example

JavaScript
2
star
99

use-svelte

A React hook for loading Svelte components.
JavaScript
2
star
100

wasm-ui-component-experiment

JavaScript
2
star