Use Svelte 5 Snippets To Reuse Markup Without Creating Components

Поделиться
HTML-код
  • Опубликовано: 21 ноя 2024

Комментарии • 115

  • @kimeiga
    @kimeiga 28 дней назад +1

    This was crazy. I learned something new every single second.

  • @gamecast4432
    @gamecast4432 2 месяца назад +1

    Svelte documentation is really great and easy to understand, but man. It's even better when i watch your videos, they're even better than the Svelte docs.
    From what i learned, slots are easy for easy and simple things, but get really mind bending when things start to get complex.
    Snippets on the other hand are easy to code in most of the situations i can think of.

  • @whotao6259
    @whotao6259 2 месяца назад +1

    Thanks for sharing 🙌
    I haven’t worked too closely with Svelte5, but my general impression of it is that it seems more complex than Svelte4.
    I think this is really just a reflection of how Svelte wants to solve more complex problems, and trying to make inherently complicated problems simple doesn’t come without compromises.
    For example, a noticeable friction point with Svelte4 is that you had to be aware of how reactivity could be denoted in multiple ways - stores with dollar signs, reactive labels, and local component variables. It’s easy enough to distinguish and organize in small projects, but it personally made organization and separation of concerns much more complex in large projects. e.g. should I bind to a store or a local variable, should I prop drill bindings or just use a shared store, should I use flux instead of binding altogether, etc?
    Runes addressed the problem above very elegantly because it effectively answered the questions above with “it can be reactive if it’s $state.” I think many viewed it as a “useState” equivalent without understanding the problems that arose from the originally “simpler” model of reactivity.

  • @SilvestreVivo
    @SilvestreVivo 2 месяца назад +2

    This is a big step ahead for Svelte

  • @lwinklly
    @lwinklly 2 месяца назад +4

    17:12 sorry WHAT? GENERICS??
    I'm in tears, this is amazing, thank you so much Svelte 5

    • @RisalFajar
      @RisalFajar 2 месяца назад +3

      It's also available in Svelte 4

    • @tobychidi
      @tobychidi 27 дней назад +1

      Generics in Vue too 😊

  • @pengain4
    @pengain4 2 месяца назад +1

    Love snippets! I needed something like this few times already in the past.

  • @kmaximoff
    @kmaximoff 2 месяца назад

    I have not watch this video yet, but gonna put thumbs up, just because I know how good your content is. I kind of want to rename it from Joy of Code to Joy of Svelte 😄 Jokes aside, thank you!

  • @miro-hristov
    @miro-hristov 27 дней назад

    Thanks! That was super Easy Peasy Lemon Squeezy 😁

  • @yeukang
    @yeukang Месяц назад +4

    Actually the slots refactor was quite mind-bending to me. So instead of using the let:row, you know create snippets in the parent, and render the snippets in the previously slot component... We still need to rely on IDE hints to know which inner snippets _should_ be defined when consuming the component, and what happens when they're not? Will {@render row(item)} give a type error / crash if not defined? 🤔

  • @punishedbarca761
    @punishedbarca761 2 месяца назад +13

    No offense to the people in this comment section but I don't think they ship svelte with any level of complexity if they think this is unnecessary. is great but these changes are a huge help for defining edge case render scenarios (that currently look gross and anti-pattern). This just another tool in the toolbox that all of your favourite component/design library creators will love. The best part is that it's opt-in. If you don't want snippets you can just ignore them :D

    • @TianYuanEX
      @TianYuanEX 2 месяца назад

      Exactly! I don't know what these guys are waffling about, as you said it's literally an opt-in feature. They can simply ignore it if they don't want to use it

    • @linkinl1
      @linkinl1 2 месяца назад +4

      I have used svelte 5 fulltime for the past 6 months, snippets sucks ass, I avoid them like the plauge. The Svelte community has just turned into a massive cope with Svelte 5. Svelte 5's reactivity model is much better than Svelte 4, but the syntax just sucks balls

    • @NickPoulos
      @NickPoulos 28 дней назад

      I ship lots of Svelte, much more complex than todos and counters -- and Svelte 5 is a step backwards with runes. It is antithetical to why people would pickup Svelte in the first place. The reasons and advantages you got from using Svelte over something like React/Vue have all but disappeared. At this point, why are you not just using React? Bunch of boilerplate, same concepts/mechanisms for props and reactivity, and Svelte's community and ecosystem are much smaller. JSX still sucks but then you can use Vue.
      All of these things are NOT opt-in, and the "old ways" will become older and older to the point where they are obsolete in Svelte 6.

    • @punishedbarca761
      @punishedbarca761 28 дней назад +1

      @@NickPoulos true, i get why you might prefer Svelte 4 if you prefer the Vue method of doing things. My main problem with Svelte 4 is readability and onboarding at scale. I like the route they've taken of clearly defining declarations, which makes code review and test writing 100% easier for me. It's also reduced a lot of my component code which I'm happy about.

  • @notnotjake
    @notnotjake Месяц назад +3

    Slot is still used for layouts correct? Where you aren't importing a snippet, just leaving the slot open to children?

  • @christophebenz
    @christophebenz Месяц назад

    LOL Patrick Bateman 😂
    You are a genius!

  • @sleepingdog12
    @sleepingdog12 27 дней назад +1

    Impressive

  • @wyndmill
    @wyndmill 2 месяца назад +8

    super useful, thanks

  • @adriablancafort
    @adriablancafort 2 месяца назад +2

    Amazing.

  • @nathnaelgetachew7379
    @nathnaelgetachew7379 2 месяца назад +8

    This just doesn't look like svelte....

    • @AlanDanielx
      @AlanDanielx 2 месяца назад

      @@nathnaelgetachew7379 noob developers always say that. Its very common

  • @mikejohneviota9293
    @mikejohneviota9293 2 месяца назад

    🎉🎉🎉 waiting for svelte 5 migration!!!!

  • @St0rMsk
    @St0rMsk 2 месяца назад +3

    Honestly. This is great. Slots are cool, we can say easy, but this is even better. I would like to see projects that people built with svelte who are not excited about svelte 5 / runes, especially the - you didnt have to learn svelte, now you do crowd. Wtf... You had to learn svelte when you eent beyond simple counter example. I do aswell have some "isues" with new APIs, but remember, you can still use the old stuff with svelte 5

  • @adhecson
    @adhecson 2 месяца назад

    Quite clean!

  • @TheHermitHacker
    @TheHermitHacker 2 месяца назад +2

    Thank you!

  • @lucasmiguel2633
    @lucasmiguel2633 2 месяца назад +11

    IMHO Svelte is losing the “it just feels natural” feeling that you got, for example, when using
    I was hoping this video to change my mind but it just made me more confident of my stance in favor of Svelte 4. Svelte 5 is Vue. Might make more sense, but you need to put more boilerplate and concepts that just make it less fun to work with

    • @JoyofCodeDev
      @JoyofCodeDev  2 месяца назад +1

      curious how is `` more natural than `{@render children()}`? not only do you live in the age of autocomplete but the upside is more than worth a couple of extra characters - the more people use Svelte and provide feedback makes it easier to know what to focus on and improve

    • @stathiskapnidis9389
      @stathiskapnidis9389 2 месяца назад +6

      ​@@JoyofCodeDev is simpler because it just looks like an html element.
      Like put this here and then something renders.
      {@render children()} makes it more ambiguous because what now you have to understand what @render does, when it does it, and what children() does, when it gets called, if it updates dynamically, etc.
      Before it was as if you were placing something there like a or a . Easy to understand.
      Now it does something. But you don't neceserily know what.
      You can learn, but before you didn't need to learn. It was intuitive

    • @JoyofCodeDev
      @JoyofCodeDev  2 месяца назад +1

      I see more beginners confused by `` and treating it like a black hole avoiding things like `let` because it's mind-bending but `{@render children()}` is more explicit and I would argue more intuitive if you understand functions

    • @lucasmiguel2633
      @lucasmiguel2633 2 месяца назад +1

      @@JoyofCodeDev you have a point there. However I wouldn’t know how to do binding using render functions

    • @亞米歐弟沒有
      @亞米歐弟沒有 2 месяца назад +2

      I think the changes are inspired by React. In Svelte 5, using {@render} and {#snippet}, you can pass them as props or treat them just like variables in JavaScript easily.
      But yeah, they should've kept the native for those people who don't need it.

  • @Ravenpicturez
    @Ravenpicturez 25 дней назад

    Do i have to manually type all the props to not get errors when using snippets?
    Example:
    type Props = {
    route: string;
    children: Snippet;
    content: Snippet;
    }

    • @JoyofCodeDev
      @JoyofCodeDev  24 дня назад

      you can do `let props = $props()` so TypeScript won't bother you if you don't care about types

  • @shapelessed
    @shapelessed 2 месяца назад +6

    If you were to compare React to Linux, Svelte woud be MacOS - Gets out of your way so you can do your job instead of fixing some niche and random issues. Now it's becoming Windows 11...

    • @JoyofCodeDev
      @JoyofCodeDev  2 месяца назад

      idk

    • @sadunozer2241
      @sadunozer2241 2 месяца назад +2

      I’m an unreal engine programmer. I build the engine from source, with 120GB debug symbols and it’s all just there. it has a DX Ilthat I love and cherish… and Rich Harris is probably the only one in the web domain who, I think, has a similar engine/plugins/game-project mindset which is simply fun and productive to work with. Svelte is Debian. It’s solid, it’s got everything you need, it’s lean as fuck

    • @shapelessed
      @shapelessed 2 месяца назад

      @@sadunozer2241 It was.

  • @davidjacobs7966
    @davidjacobs7966 2 месяца назад +3

    I don't understand some devs' arguments against a single component per file. That's supposed to be the essence of component-oriented programming. 😅

    • @JoyofCodeDev
      @JoyofCodeDev  2 месяца назад

      the real power is providing your own render function

    • @jamesmoynihan948
      @jamesmoynihan948 2 месяца назад +1

      @davidjacobs7966 I love SFCs, but snippets still have a use when you repeat a pattern within a component. Does it really need to be a whole new component just to avoid repeating myself in the markup? I'd argue there's a decent amount of code repetition when the code gets over-fractured as well.

  • @nabeelalihashmi
    @nabeelalihashmi 2 месяца назад +16

    I might be wrong, but I feel like Svelte is moving away from what it made Svelte 😥

    • @JoyofCodeDev
      @JoyofCodeDev  2 месяца назад +8

      you're wrong 😄

    • @kollpotato
      @kollpotato 2 месяца назад +1

      no one holds you back from using the old versions of svelte, if they work for you then use them without hesitations

    • @jamesmoynihan948
      @jamesmoynihan948 2 месяца назад +2

      What specifically do you think is worse or missing? For me, runes and snippets solve a lot of pain points that arise when building a larger app.

    • @nabeelalihashmi
      @nabeelalihashmi 2 месяца назад

      @@JoyofCodeDev 😎 😎 you're {#probably right}🤣

    • @nabeelalihashmi
      @nabeelalihashmi 2 месяца назад

      @@jamesmoynihan948 Moving away doesn't mean worsening the matters. That's a good thing if runes are solving inconsistency issues.
      But, my theory is, many people, chose Svelte due to simplicity. And
      let x = 10 is simplicity, and export let y in component is also a simplicity.
      a question, when talking about larger applications, what number of loc defines "larger application"?

  • @nghiemduonghung9750
    @nghiemduonghung9750 Месяц назад

    Is this the solution for layout name slot?

  • @ChadcholMeesaeng
    @ChadcholMeesaeng Месяц назад

    let's see paul allen's snippet

  • @Donald_Mustard_FN
    @Donald_Mustard_FN 2 месяца назад

    What code editor is this ??

  • @pixiedev
    @pixiedev 2 месяца назад +3

    In svelte 5 just the slot-children is different else everything is same as vue 😅

    • @JoyofCodeDev
      @JoyofCodeDev  2 месяца назад +7

      I find this amusing because the future of Vue (Vapor) looks more like Svelte

    • @m_raez
      @m_raez 2 месяца назад +3

      @@JoyofCodeDev if something works well, use it or copy it for your integration. Developers copy from each other and library/framework authors do the same. If it solves problems and provides good abstraction or ergonomics, why not?

    • @rafalpotasz
      @rafalpotasz 2 месяца назад +1

      ​@@m_raez I don't think JOC is saying anything that would disagree with you tbh. I agree though. Copy the best things, learn their patterns, improve your own thinking for free essentially?

    • @JoyofCodeDev
      @JoyofCodeDev  2 месяца назад +3

      everyone wins

    • @NickPoulos
      @NickPoulos 28 дней назад

      You didn't know? React/Vue/Svelte are all just merging into one big ball of the same features with slightly different syntax now!

  • @mynameisnotshane6501
    @mynameisnotshane6501 2 месяца назад +1

    Coming from react js world i feel like i came out of a cave era

  • @NatoBoram
    @NatoBoram 2 месяца назад

    Hm... I'm not really convinced. Making new components is just good project hygiene tbh. I'll have to test it to see where I have a use case for that.
    That said, the experience looks great if there's a good use case for them
    Oh, generic! Now, I see. That's game changing!

  • @DanteMishima
    @DanteMishima 2 месяца назад +1

    Yeah... I won't be using svelte 5.... It's too weird for me.

  • @icemelt7ful
    @icemelt7ful Месяц назад

    so we're 50% there on the way to make react

  • @mikemcculley
    @mikemcculley 2 месяца назад

    "That's 'bone.' Unfortunately, it's not a named color in CSS."

  • @leigh321
    @leigh321 2 месяца назад

    Lower case boom 🥹

  • @htichpirate
    @htichpirate 24 дня назад

    why have a footer tag in snippet and around the render also one? That's result a footer tag in footer tag..

    • @JoyofCodeDev
      @JoyofCodeDev  24 дня назад

      simple mistake

    • @htichpirate
      @htichpirate 23 дня назад

      @@JoyofCodeDev ah, ok, thank you for clarification :)

  • @GeraldScholz
    @GeraldScholz 2 месяца назад +3

    Lol snippets are awful 😂 sorry

  • @m_raez
    @m_raez 2 месяца назад

    createRawSnippets looks rather similar to Vue's defineComponent to create Components in JS/TS without SFC-files.

    • @JoyofCodeDev
      @JoyofCodeDev  2 месяца назад

      one step closer to having a custom renderer 🔥

  • @icemelt7ful
    @icemelt7ful Месяц назад

    ezy pezy lemon squezy

  • @nsoneu
    @nsoneu 28 дней назад

    5 lemon squeeze so far... my ears are bleeding 😂.. great content though.

    • @JoyofCodeDev
      @JoyofCodeDev  28 дней назад +1

      🍋

    • @nsoneu
      @nsoneu 28 дней назад

      @@JoyofCodeDev thank you for your video i also meant to write 🙌

  • @roiborromeo7921
    @roiborromeo7921 2 месяца назад +1

    This isn't what makes Svelte promising.

  • @cromrin
    @cromrin 2 месяца назад +1

    I personally dont like snippets I like more the lets put it into its separate component type of stuff. If I use component on multiple places lets put it into /components if I use It only in specific page I put it next to +page.svelte inside _components. The code is so much clearer to read the less lines of code it has. I am that guy who does like if all code lines of a file fits into his view 0 +/-125 lines of code.

  • @Holobrine
    @Holobrine 2 месяца назад +1

    TIL Svelte 5 supports generics, very cool

    • @JoyofCodeDev
      @JoyofCodeDev  2 месяца назад

      it also has native TypeScript support

    • @imho7992
      @imho7992 2 месяца назад

      Yeah this was a surprise. Does this mean i don’t need to define a type prop for my components anymore? 🤯

  • @realderek
    @realderek 2 месяца назад

    This concept seems "alright" to me. I think the problem is that developers loved the allure of a straight-forward framework like Svelte. The problem is, for more complex use cases it wasn't as simple. Now that it's becoming more flexible to accommodate the complex use cases (such as UI libraries), it's losing the appeal of being a "straight-forward" framework.

  • @Floubadour
    @Floubadour 25 дней назад

    Easy peasy lemon squeezy. Please, do not repeat it so many times!

    • @JoyofCodeDev
      @JoyofCodeDev  24 дня назад

      that's going to be easy peasy lemon squeezy

  • @davidjacobs7966
    @davidjacobs7966 2 месяца назад +2

    when is ready Svelte 5?

    • @JoyofCodeDev
      @JoyofCodeDev  2 месяца назад +2

      I predict the release at next Svelte Summit 🔮

    • @AlanDanielx
      @AlanDanielx Месяц назад

      @@JoyofCodeDev Mission Failed

    • @AlanDanielx
      @AlanDanielx Месяц назад

      don't wait until svelte 5, try out the pre-release, it's not going to change a lot

  • @HIChoi-q8e
    @HIChoi-q8e 27 дней назад

    Ugly. If you want to use nested component outside of the component, pull out the component is clear way to reuse.

  • @stathiskapnidis9389
    @stathiskapnidis9389 2 месяца назад +26

    svelte 5 continues to disapoint me

    • @ok-alarm
      @ok-alarm 2 месяца назад

      heyy, why?

    • @danko95bgd
      @danko95bgd 2 месяца назад +4

      @@ok-alarm because snippets suck

    • @stathiskapnidis9389
      @stathiskapnidis9389 2 месяца назад +14

      ​@@ok-alarm slot was so much easier. Now I have to do children() and other stuff like that.
      The beuty of svelte was it's simplicity.

    • @Dhalucario
      @Dhalucario 2 месяца назад +4

      I think using children() is nicer because you have an unified way to get the child elements in your component in js and html

    • @JoyofCodeDev
      @JoyofCodeDev  2 месяца назад +10

      you can mix them together if you want