Master The Svelte Context API

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ม.ค. 2025

ความคิดเห็น • 72

  • @JoyofCodeDev
    @JoyofCodeDev  หลายเดือนก่อน +13

    I need to make a correction regarding the usage of unique keys for context because when you use the Svelte Context API, the context is not shared across component instances, and a unique key mostly serves to avoid context key collisions: joyofcode.xyz/master-the-svelte-context-api#use-a-unique-key-for-context.

    • @nigelnyakudya2262
      @nigelnyakudya2262 หลายเดือนก่อน

      @@JoyofCodeDev thanks

    • @nigelnyakudya2262
      @nigelnyakudya2262 หลายเดือนก่อน

      @@JoyofCodeDev I will try it and give you feedback

  • @cole5981
    @cole5981 หลายเดือนก่อน +4

    The Context API can be great, especially when making a complex library, or a set of tightly-knit nested components. It's such a relief to be able to pick and choose which data/contexts you need in nested components, without needing to refactor all parent components constantly.
    However, I see a lot of people reach for the Context API to avoid passing props through wrapper/UI classes. For example: having forms inside modal/dialog components. Don't forget about slotted components! They will keep your code clean, modular, and readable (all without the needs for props!)

    • @barchuk422
      @barchuk422 หลายเดือนก่อน

      what are slotted components?

    • @cole5981
      @cole5981 หลายเดือนก่อน

      @@barchuk422 I think they're called "snippets" now. If you check out the docs, under "passing snippets to components", there's an example with "".
      From there, you can nest code/logic without leaving your root file.

  • @camoman1000
    @camoman1000 หลายเดือนก่อน +2

    This actually comes in really handy with the project I'm working on currently and now I can make a context at home that works outside of svelte component lifecycles

    • @JoyofCodeDev
      @JoyofCodeDev  หลายเดือนก่อน +1

      just be careful because it doesn't work the same, and you're creating global state - the original idea I had was to create the context inside `` because it's at least scoped to that module, but I picked the easier route to not confuse people with concepts unrelated to the Context API

    • @camoman1000
      @camoman1000 หลายเดือนก่อน

      @JoyofCodeDev oh that's a good point forgot about module. But I'm actually going a different route to make using it easier. I'm porting framer-motion from react to svelte and keeping the context usage straight was messy. So I was thinking of using the global, but then I watched the rest of your video and learned about the relationship with components. So I decided to just wrap similar to the svelte docs recommendation.

  • @ahmedhossamdev
    @ahmedhossamdev หลายเดือนก่อน +4

    That's very cool! Can you create a big project, please, to apply these concepts in a big project with the new Svelte 5 syntax?

  • @barchuk422
    @barchuk422 หลายเดือนก่อน +1

    yesssss this is all I wanted

  • @desLunes
    @desLunes หลายเดือนก่อน +2

    Not related to this video, but you're the most knowledgable person i know regarding Svelte.
    I'm using Svelte's parse function (from 'svelte/compiler') to get the code as an AST. I perform some manipulation to the code (for example, adding attributes to the DOM). But I didn't find anything to print the AST back to code. There's an unofficial project for that, which is called svelte-ast-print, but it's not reliable enough to use it on the whole codebase.
    I'm wondering if there's something Svelte itself provides to do that, I mean, Svelte's compiler uses that function so it needs to print it back at some point... I hope this message gets read, have a nice day and ty for reading.

    • @JoyofCodeDev
      @JoyofCodeDev  หลายเดือนก่อน +1

      no idea maybe it would be worth looking at what Svelte does

    • @paoloricciuti
      @paoloricciuti หลายเดือนก่อน +1

      Nope svelte doesn't need to print the svelte AST because the svelte AST is compiled to JS not to svelte.
      For the migration script (which is infact modifying your svelte code) instead of using AST manipulation we use the AST to modify the code string with MagicString...what are you specifically trying to do?

    • @desLunes
      @desLunes หลายเดือนก่อน +1

      ​@@paoloricciuti The project is called "Tailwind Attributify". Basically, the goal is to let you write CSS classes in other attributes than the class attribute in your codebase, and the preprocessor I'm writing is putting back those classes in the class attribute when building.
      For example, if you have a lot of classes with the prefix hover: in tailwind, you can put them in the hover attribute and the preprocessor will put them back in the class attribute.
      I use the markup() hook from Svelte's preprocessors to intercept code, but it gives me the code as string and expects me to return it as a string too. I use Svelte's AST to walk through the tree and detect attributes and manipulate their content. But I didn't find anything to print it back.
      I guess Svelte's AST is not meant to be used in a preprocessor, but it would be a great addition and make Svelte more customizable.

    • @desLunes
      @desLunes หลายเดือนก่อน

      @@JoyofCodeDev Thank you, have a great day.

  • @ucielsola
    @ucielsola หลายเดือนก่อน

    yes!!! Thank you 😄

  • @levicoderman
    @levicoderman หลายเดือนก่อน +14

    0:45 🤨 📸

    • @Bhide.Svelte
      @Bhide.Svelte หลายเดือนก่อน +1

      🤣🤣🤣

  • @Atimska
    @Atimska 13 ชั่วโมงที่ผ่านมา

    I never knew you could export a function from a component like you did with your Canvas (pre-contextifying it).

  • @iulikdev
    @iulikdev หลายเดือนก่อน +2

    You like bananas! 🤣🤣 Fantastic content!

  • @mikejohneviota9293
    @mikejohneviota9293 หลายเดือนก่อน +1

    I use this it's very very OP 🎉

  • @Sockheadrps
    @Sockheadrps หลายเดือนก่อน +1

    I love your videos man been watching for a few years. I still suck at svelte though.

    • @JoyofCodeDev
      @JoyofCodeDev  หลายเดือนก่อน +2

      same

    • @Sockheadrps
      @Sockheadrps หลายเดือนก่อน +1

      @ no way man you’re the best. I really enjoy your style of video I wish more people were turned on to svelte, you put out great content. Animation is super cool too!

  • @abtix
    @abtix หลายเดือนก่อน +1

    These videos are so good, I love that you show the JavaScript implementation and explain what's happening under the hood.
    Will you be making a video on $effect? I understand what it does, but I don't really know where to use it, and I'm kind of just wondering what things are now easy to solve via $effect. Also, in your "Avoid Async $effect" video you mentioned that $effect should be used instead of onMount and that we should let onMount die, but then how do I handle the case where I want running only once in the beginning? Do I continue using onMount or do I just make 2 different $effects, one that has nothing reactive in it?

    • @gorbulevsv
      @gorbulevsv หลายเดือนก่อน

      In a small project, I changed the onmount to an effect, didn't check the code, and later, having added console output to the effect for verification, I saw with horror that the effect was yanking the server an infinite number of times.

    • @gorbulevsv
      @gorbulevsv หลายเดือนก่อน

      I realized that I had to be more careful with the effect.

    • @JoyofCodeDev
      @JoyofCodeDev  หลายเดือนก่อน

      I have an entire video on effects: th-cam.com/video/HRz_rU2BlZc/w-d-xo.html

    • @abtix
      @abtix หลายเดือนก่อน

      @@JoyofCodeDev Ohhh I never saw that. Thank you very much

    • @abtix
      @abtix หลายเดือนก่อน

      @@gorbulevsv Yeah, I learnt that the hard way in Svelte 4, using the $: {} and dumping a bunch of variables that I didn't realize would retrigger it, making it run in a loop. Just gotta be extra careful when using $effect.

  • @good-dev-student
    @good-dev-student หลายเดือนก่อน +1

    Hello, i have a question if i have a user obj and i wanna to save it in +layout.server.svelte from db to Context API or store ?
    so i can later use it in a navbar ?

    • @good-dev-student
      @good-dev-student หลายเดือนก่อน

      Edit i found that i an use it:
      Based on the code and your requirement to store and access the user object without passing it as props, I recommend using Svelte's context API since your application appears to be using server-side rendering (SSR). This is the safer approach compared to global stores to avoid state sharing between different users on the server.

    • @JoyofCodeDev
      @JoyofCodeDev  หลายเดือนก่อน

      yeah I have video on that later

  • @justintie
    @justintie หลายเดือนก่อน +2

    Does Svelte need an async state lib such as Thunk or Saga?

    • @justinoneill2837
      @justinoneill2837 หลายเดือนก่อน

      No his last video before this talks about the pitfalls when using *async/await* in Svelte 5 but you might want to check out @sheepdog/svelte

    • @JoyofCodeDev
      @JoyofCodeDev  หลายเดือนก่อน +2

      you don't need complicated state management solutions in Svelte

    • @justintie
      @justintie หลายเดือนก่อน +1

      @@JoyofCodeDev ok, thanks

    • @StarsOfMinecrafttr
      @StarsOfMinecrafttr หลายเดือนก่อน +2

      @@justintie if there is no problem then you dont need a solution right?

  • @nigelnyakudya2262
    @nigelnyakudya2262 หลายเดือนก่อน

    Hi brother, thanks for the great content. I need help.. Just started using Svelte 5, now now not sure how to do certain things securely. Im creating a registration with different steps on different pages, how best control user state between these pages?

    • @JoyofCodeDev
      @JoyofCodeDev  หลายเดือนก่อน +1

      you can use the `$page` store to share the data or use the Context API in SvelteKit

  • @serg888fert4
    @serg888fert4 วันที่ผ่านมา

    can context be reactive?

  • @Shoutlikeatwebpunktd
    @Shoutlikeatwebpunktd หลายเดือนก่อน +5

    I've been programming for over ten years and just learned that you can omit the parentheses when calling a constructor in js ... it looks so wrong 🙈😂

    • @justinoneill2837
      @justinoneill2837 หลายเดือนก่อน

      Can you give an example or point to the time?

    • @tatater56
      @tatater56 หลายเดือนก่อน

      ​@@justinoneill2837
      new Date();
      new Date; // same thing
      It's syntactic sugar and only applies for constructor calls with no arguments. There are some potential pitfalls though.
      new Date().toString(); // ok
      new Date.toString(); // throws "TypeError: Date.toString is not a constructor"

    • @JoyofCodeDev
      @JoyofCodeDev  หลายเดือนก่อน +1

      if you're talking about `new Map` that was a mistake but Prettier fixed it on save

    • @Shoutlikeatwebpunktd
      @Shoutlikeatwebpunktd หลายเดือนก่อน +1

      @@JoyofCodeDev I just tried it in the browser console and it seems to work just fine without parentheses 🤷‍♂

    • @youarethecssformyhtml
      @youarethecssformyhtml หลายเดือนก่อน +1

      Yes you can create an instance without using parentheses but it looks so weird so I avoid it

  • @greendsnow
    @greendsnow หลายเดือนก่อน +1

    Most datagrid libraries have problem with svelte 5...

  • @good-dev-student
    @good-dev-student หลายเดือนก่อน

    Thank you so much my hero, why you don't join to Svelte Dev teams maybe we see news in motion svelte main framework ?

    • @JoyofCodeDev
      @JoyofCodeDev  หลายเดือนก่อน +1

      I'm already a Svelte ambassador 😄

  • @jazza231
    @jazza231 หลายเดือนก่อน +1

    Programming naming schemes summed up at 0:38

  • @Alfrednohike9909
    @Alfrednohike9909 หลายเดือนก่อน +1

    teach us Storybookjs

  • @mohabedr5030
    @mohabedr5030 หลายเดือนก่อน +1

    this is not a reusable component anymore, your component is coupled to the state now

  • @joeseabreeze
    @joeseabreeze หลายเดือนก่อน +2

    I’ve noticed that you never use semicolons in your code, even with TS. Do you setup rules somewhere to help mitigate potential errors? And how do you prevent ai code-complete from inserting semicolons? I personally do not like semicolons. It’s by far the most overused and useless character in a programming language 😅

    • @justinoneill2837
      @justinoneill2837 หลายเดือนก่อน +1

      Following

    • @JoyofCodeDev
      @JoyofCodeDev  หลายเดือนก่อน +1

      yeah I have them disabled in my editor settings under the Prettier config and I never ran into problems: github.com/mattcroat/uses

    • @DriftJunkie
      @DriftJunkie หลายเดือนก่อน +1

      If you are against semicolons, you should try writing a simple interpreter to see the performance loss from ambiguous declaration delimiting

    • @justinoneill2837
      @justinoneill2837 หลายเดือนก่อน

      @@DriftJunkie svelte compiler fixes all that yeah?

    • @DriftJunkie
      @DriftJunkie หลายเดือนก่อน

      @@justinoneill2837
      No.
      Sorry, but I won't be able to explain the issue here. Too long of a topic.
      I can just tell you, that learning basic Computer Science helps alot in understanding, why some seemingly "annoying" things exist in coding.

  • @Sockheadrps
    @Sockheadrps 2 วันที่ผ่านมา

    Context at home lmao