Svelte Store: Reactive context using Svelte Store

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 ก.พ. 2025
  • How do we make context value reactive?
    Context values seemed to be stuck once initialised, how does making the changing values available through context?
    ---
    💻 REPL: svelte.dev/rep...
    📚 Related Resources
    getContext and setContext svelte.dev/tut...
    🥰 Support me www.buymeacoff...
    🔗 Want to learn more from me? Check out these links:
    Twitter: / lihautan
    Blog: lihautan.com/
    Newsletter: lihautan.com/n...
    #svelte #sveltestore
    Thanks for watching 🥰 -- with love, Li Hau

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

  • @combinio9533
    @combinio9533 ปีที่แล้ว

    Very good tutorial, thanks for sharing!

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

    Thank you so much 👍👍👍It helped !!!!

  • @wawmsey
    @wawmsey 2 ปีที่แล้ว +1

    really helpful, thanks!

  • @AbhinavKulshreshtha
    @AbhinavKulshreshtha 3 ปีที่แล้ว +9

    Can you do a full svelte project with all the recommended practices. Not REPL or sapper, but teach us how you would go about structuring a svelte front end from scratch.

    • @lihautan
      @lihautan  3 ปีที่แล้ว +5

      yes. will definitely do that

  • @munawwiruljamal4149
    @munawwiruljamal4149 3 ปีที่แล้ว +2

    How come I never thought of this haha
    Thank you master

  • @Gibbu_
    @Gibbu_ 3 ปีที่แล้ว +1

    Legend. Thanks for this!

  • @funnynews341
    @funnynews341 ปีที่แล้ว

    can we getContext one value many times in many child companents?

  • @softgripper
    @softgripper 3 ปีที่แล้ว

    I'm getting flashbacks to discord from a day or 2 ago :D

    • @lihautan
      @lihautan  3 ปีที่แล้ว

      yup, i think ppl are discussing about it few days ago

  • @tristanlucas5556
    @tristanlucas5556 3 ปีที่แล้ว

    Just for the sake of discussion, it seems to me the context you are trying to make reactive is little contrived. Because as you said at the beginning, you could have an external svelte store and it would work the same as your final result.
    The Context example provided by the Tutorial needs the context to provide a map, so that each hard coded MapMarker has a map to be added on.
    The Map object is not really a Svelte Store in the way you have defined it, with a subscribe and/or set function. So if you want to show how to make a Context reactive, it would be great to transform a non store-like as the Map. But I'm not sure it is useful or possible as the Map object cannot hold MapMarkers. And maybe you can transform it into a writable store instead of using a context.
    A possible idea of implementation: when I touch the map at a position where there is no marker, I create a new one. When I touch a marker, I remove it.
    I would use Context to create a unidirectional way to share data without the need or the possibility to update the source, while store should be used when you want 2 way binding between components and a store.
    Moreover, Context provides a way to scope the access to some data to a specific hierarchy of components while store is more opened to any components willing to use it.
    Please correct me if I'm wrong in my thoughts. Just wanted to give my 2 cents.
    Anyway, you got me thinking a little bit so thank you for this video.

    • @lihautan
      @lihautan  3 ปีที่แล้ว

      here's a video on how i think when / why i would use store / context / both.
      th-cam.com/video/p4GmT0trCPE/w-d-xo.html
      bring it back to the map + map marker example, my question would be, would you expect to have multiple maps on the app at the same time?

    • @tristanlucas5556
      @tristanlucas5556 3 ปีที่แล้ว +1

      @@lihautan I wanted to stay in the context of the tutorial, so it was just one instance of the map in the app. So after watching the video you have pointed out, I guess that I would use a single local store to manage the map markers, if I want to add or remove them.
      If I have multiple maps on the app, I would use :
      - 1 global store if the maps should have the same markers
      - multiple context stores if I want the maps to display their distinctive markers
      I hope I understand your store/context video well enough.
      Thanks for your interest and your super videos.

    • @lihautan
      @lihautan  3 ปีที่แล้ว +1

      @@tristanlucas5556 yup, I think you got it!

  • @mupmuptv
    @mupmuptv 3 ปีที่แล้ว +1

    同じstore. Storeの方がいい