Fireship · @fireship
Svelte in 100 Seconds
Uploaded April 13, 2021 · Length 2:12
Summary
Svelte explained as a JavaScript tool for building UI components with a compiler that converts declarative code to imperative code for high performance.
- svelte
- javascript framework
- ui components
- compiler
- declarative code
- reactive state
- component communication
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
svelt it's a javascript tool for
- 0:02
building ui components just like react
- 0:04
angular or vue but unlike its peers
- 0:06
would ship a javascript runtime to the
- 0:08
browser to make your code work
- 0:09
svelte is a compiler it converts the
- 0:11
declarative code that you'd love to
- 0:12
write as a developer
- 0:14
into imperative code that works with
- 0:15
native browser apis
- 0:17
as a result you get highly performant
- 0:19
code in a very small package
- 0:20
but most importantly it's the only
- 0:22
javascript framework that's actually
- 0:23
enjoyable to use
- 0:27
we create components in dot spelled
- 0:29
files which contain three main parts
- 0:31
a script for your javascript code which
- 0:33
can also be typescript
- 0:34
a style tag for your css which can also
- 0:37
use a preprocessor like sas and the main
- 0:39
template represented as regular html
- 0:42
but it's no ordinary html it's been
- 0:44
empowered with all kinds of extra syntax
- 0:46
for writing declarative code
- 0:47
need reactive state simply define a
- 0:49
variable with the let keyword then
- 0:51
reference it dynamically in the html
- 0:53
using braces if we want to change the
- 0:55
state we can do that by defining a
- 0:56
function then listen
- 0:58
to an event in the dom with on and bind
- 1:00
the function to it as the handler
- 1:02
now when the value of that variable
- 1:03
changes the component will automatically
- 1:05
re-render to show the updated state
- 1:07
in many cases you'll need to run
- 1:09
conditional logic or loops in your
- 1:10
template
- 1:11
instead of fighting against javascript
- 1:12
functions it empowers you with a syntax
- 1:14
where you can clearly create an if else
- 1:16
statement
- 1:17
or a for each loop when working with a
- 1:19
list now when it comes to
- 1:20
cross-component communication
- 1:22
spelt provides multiple different
- 1:23
strategies for sharing data between
- 1:25
components to pass data from a parent to
- 1:27
child you can use props by adding the
- 1:29
export keyword to a variable and now you
- 1:31
can pass data into this component
- 1:33
and if you have a ton of props you can
- 1:34
use the spread syntax to keep your code
- 1:36
looking fit and healthy
- 1:37
now for more complex component trees you
- 1:39
have a context api just like react
- 1:42
and if that wasn't enough spelt also has
- 1:43
a really nice mechanism called stores
- 1:46
which are like observables that can be
- 1:47
shared anywhere in the component tree
- 1:49
and subscribed to in the template by
- 1:51
putting a dollar sign in front of them
- 1:52
after you've built an awesome ui you can
- 1:54
then use the compiler to convert it to
- 1:56
vanilla javascript
- 1:57
and if you're building a full-blown web
- 1:59
application you can use spelt kit to


