Fireship · @fireship

Svelte in 100 Seconds

Uploaded April 13, 2021 · Length 2:12

Svelte in 100 Seconds

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.

  1. 0:00

    svelt it's a javascript tool for

  2. 0:02

    building ui components just like react

  3. 0:04

    angular or vue but unlike its peers

  4. 0:06

    would ship a javascript runtime to the

  5. 0:08

    browser to make your code work

  6. 0:09

    svelte is a compiler it converts the

  7. 0:11

    declarative code that you'd love to

  8. 0:12

    write as a developer

  9. 0:14

    into imperative code that works with

  10. 0:15

    native browser apis

  11. 0:17

    as a result you get highly performant

  12. 0:19

    code in a very small package

  13. 0:20

    but most importantly it's the only

  14. 0:22

    javascript framework that's actually

  15. 0:23

    enjoyable to use

  16. 0:27

    we create components in dot spelled

  17. 0:29

    files which contain three main parts

  18. 0:31

    a script for your javascript code which

  19. 0:33

    can also be typescript

  20. 0:34

    a style tag for your css which can also

  21. 0:37

    use a preprocessor like sas and the main

  22. 0:39

    template represented as regular html

  23. 0:42

    but it's no ordinary html it's been

  24. 0:44

    empowered with all kinds of extra syntax

  25. 0:46

    for writing declarative code

  26. 0:47

    need reactive state simply define a

  27. 0:49

    variable with the let keyword then

  28. 0:51

    reference it dynamically in the html

  29. 0:53

    using braces if we want to change the

  30. 0:55

    state we can do that by defining a

  31. 0:56

    function then listen

  32. 0:58

    to an event in the dom with on and bind

  33. 1:00

    the function to it as the handler

  34. 1:02

    now when the value of that variable

  35. 1:03

    changes the component will automatically

  36. 1:05

    re-render to show the updated state

  37. 1:07

    in many cases you'll need to run

  38. 1:09

    conditional logic or loops in your

  39. 1:10

    template

  40. 1:11

    instead of fighting against javascript

  41. 1:12

    functions it empowers you with a syntax

  42. 1:14

    where you can clearly create an if else

  43. 1:16

    statement

  44. 1:17

    or a for each loop when working with a

  45. 1:19

    list now when it comes to

  46. 1:20

    cross-component communication

  47. 1:22

    spelt provides multiple different

  48. 1:23

    strategies for sharing data between

  49. 1:25

    components to pass data from a parent to

  50. 1:27

    child you can use props by adding the

  51. 1:29

    export keyword to a variable and now you

  52. 1:31

    can pass data into this component

  53. 1:33

    and if you have a ton of props you can

  54. 1:34

    use the spread syntax to keep your code

  55. 1:36

    looking fit and healthy

  56. 1:37

    now for more complex component trees you

  57. 1:39

    have a context api just like react

  58. 1:42

    and if that wasn't enough spelt also has

  59. 1:43

    a really nice mechanism called stores

  60. 1:46

    which are like observables that can be

  61. 1:47

    shared anywhere in the component tree

  62. 1:49

    and subscribed to in the template by

  63. 1:51

    putting a dollar sign in front of them

  64. 1:52

    after you've built an awesome ui you can

  65. 1:54

    then use the compiler to convert it to

  66. 1:56

    vanilla javascript

  67. 1:57

    and if you're building a full-blown web

  68. 1:59

    application you can use spelt kit to