Mastering Typescript State using Zustand

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ก.ย. 2024
  • Continuing on with Typescript we are starting a series on React state management where we take the same To-Do list and implement it using a bunch of different state managers. This time we are looking at Zustand!
    Zustand: zustand.surge.sh/
    Starting code: github.com/jhe...
    Finished code: github.com/jhe...
    If you liked this video, buy me a coffee! www.buymeacoff...
    Hope you folks enjoy this!
    👉 If you enjoy this video, please like it and share it.
    👉 Don't forget to subscribe to this channel for more updates: bit.ly/2E7drfJ
    👉 Weekly free newsletter: www.jackherrin...
    👉 Discord server signup: / discord
    💢 Watch our other videos:
    💟 Hacking your Github contribution graph: • Hacking Your Github Co...
    💟 Build a KK Slider Slider: • Build A K K Slider Sli...
    Thank you for watching this video, click the "SUBSCRIBE" button for stay connected with this channel.
    Subscription Link: bit.ly/2E7drfJ
    #reactjs #typescript

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

  • @bacon37460
    @bacon37460 ปีที่แล้ว +8

    Unbelievable replacement for the nightmare redux boilerplates. Thanks man for this quick demo.

  • @rohitaggarwal8776
    @rohitaggarwal8776 2 ปีที่แล้ว +9

    Man really love your work. I have been watching your videos over a week for Typescript and loved every videos for great explanation. Keep up the good work.

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

    @Jack Herrington .. you are a champion ! . I have now started learning from the video before seeing the docs :-) and then reading them there after. Started on zustand and the first place to look for was your video ..

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

    That rocket in the background is way to cool. Love your channel Jack. Such high-quality content. Can't wait to see more from you. See you soon my friend!

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

    Wow - `cmd +k, cmd + i`

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

    Thank you so much! I love your calming voice and teaching style. Subcribed 🤟🤟

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

    Got me out of a pinch yet again. Thank you.

  • @presida3927
    @presida3927 2 ปีที่แล้ว +5

    4:10 Uncle Jack? in Zustand we don't have to destructure the whole state inside our function, isn't it?

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

      I think you're right, we don't need to destructure the object. My bad.

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

      Yes, I think that a main difference from Redux

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

    Love zustand. Thanks for introducing me to it. Also, very off-topic, I recently saw Richard Basehart on the old tv series Voyage to the Bottom of the Sea. I thought this guy must be jherr's dad!

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

      Actually, yeah, in some ways my dad looked a lot like that guy.

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

    Information in this video is really useful for me. thank you so much !!

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

    Very nice intro to zustand in react for state management.

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

    You make an awesome quality content. The voice is perfect 👌🏻

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

    So Old Upload date....yet still using the Loving TS with zustand.....Where as others naturally uses JS 1year back......I neede Zustand in one of my project...had to be TS.....Thanks for the Rich Content......
    Yours videos on react state mamagement and hooks made me subbed u .....Nice ones❤❤❤❤❤

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

    This has to be one of your fastest state management crud+async video, I haven't even finished my coffee while watching this :) , do you have a poll to decide what you'll be using next? I'll vote for effector.

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

      I'll check it out and maybe add it to the list. What do you like about it?

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

      ​@@jherr their api makes it easier to do side effects, and the store + event paradigm is intuitive, just the readability really.

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

      @@romanmunar Looking at it right now actually. I'll do a video on this. It's looks novel.

  • @hamatoYogi
    @hamatoYogi 3 ปีที่แล้ว +4

    Always great video Jack!
    I love the pace of your latest stuff. Keep it up! 💪🏽

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

      Thank you! And thanks for all your feedback!

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

    nice tutorial and I like the beauty of typescript with zustand implementation... 😊

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

    You're absolutely the best.

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

    Yeah, really like of Zustand.

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

    Now we need a persist video with localstorage sessionstorage and hydration nextjs issues

    • @jherr
      @jherr  20 ชั่วโมงที่ผ่านมา

      Yeah, you'll need to employ the supressHydrationErrors flag, or put the localstorage/sessionstorage/etc. into a useEffect so that it runs after hydration.

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

    A bit late to the party, but it was exciting to check current number of Zustand's downloads and see that it's over 600K a year later.
    Thanks for the overview!

    • @jherr
      @jherr  2 ปีที่แล้ว

      Absolutely!

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

    Another fantastically valuable video. I'm planning out a new project, zustand is probably leading the charge for now, but I saw a user on another post recommend Effector, that looks pretty great as well. It really clicked with how I think. I'd love to see you do a video it as well. Jack++

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

      You might want to join today's livestream if you want to hear about Effector. I'm going to experiment and record a video on Effector live while you watch. I may get a video out of it, or not. But at the very least you'll learn about Effector.

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

      @@jherr ah bummer! I missed it. Is there a way to view it? Will there be a video on Effector? Thanks!

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

      @@slikk66 There will be! Probably releasing this week after the Wednesday video.

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

    Love your work Jack, keep it up.

  • @Kunal-jp8tn
    @Kunal-jp8tn ปีที่แล้ว +1

    Great tutorial man !

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

    Very nice, thank you this is very helpfull to me

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

    I don't understand the addTodo function. Is that recursive or what?
    Great video!

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

      It's not recursive, addTodo are another function defined above store, you can check on the finished code

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

      @@iolumin I see now, thanks! did not see that before and I was so confused

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

    Great video! Thanx a lot!

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

    Cool, thanks!

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

    you are the best!!!

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

    love ur content, please never stop ❤️

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

    You're good.

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

    Thanks Jack. Quick question, do you have any series about sharing state across micro frontends?

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

      I do! Check this out: th-cam.com/video/tFDvEITdJZ8/w-d-xo.html I have other videos as well, but this is the most recent.

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

    Love your VSCode theme & font.
    Any chance you can share your setup, please?
    Thanks!

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

      The font is Cascadia Code and im not so sure about the theme but i think its The Night Owl Theme

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

    Nice tutorial. I like your videos a lot they are short and go straight to the point. Do you know something about getting a slice of context with the context api without unecessary renderings ? I am using a redux-like pattern in my own project but when consuming a context from a component it will rerender every time there is a change in the whole context even if I use a slice of this context. Ive tried some hoc patterns to pass down the desired slice of the context but i find very hard to type those high order components

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

      Check out the docs, github.com/pmndrs/zustand, if select a certain field then you will only get updates when that field changes.

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

    Thank You for the fascinating overview. For such a small application this solution looks elegant and clean. I have a question though. Don't you think handling an input value in a global store will introduce a performance overhead, it's temporary data, but for each keystroke, we create a new global state object by spreading a previous state and simply adding chars 1 by 1. We've just created an n-amount of intermediate objects that will be garbage collected just to change an input value. What are your thoughts on this?

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

      Well, there is only the current state, any previous states can be garbage collected. Also with selectors the updates are very efficient. Only the components that need a particular changes will get re-rendered if you have written your selectors properly.

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

      ​@@jherr Well, I'm just under the impression from this video th-cam.com/video/ip51Y5v3WLk/w-d-xo.html I never bothered myself thinking of these things, but I know You're also a great fan of performance and optimization techniques, so that's why I'm asking.

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

      @@monastyrskiiden I'm not gonna watch a whole video on it. But if he's talking about lots of object creation through destructuring in loops that would be inefficient, but that is at a large scale. Updating with every keystroke is not going to be a noticeable burden on the VM.

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

      ​@@jherr Thank You for clarifying this.

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

    Why would you use Zustand vs Jotai? I'm comparing the two for my project. It looks like Jotai lets you wrap a Zustand store as well? Just made me even more confused haha

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

      Yeah, Jotai has a lot of different atom types. My thinking currently is that if it's for a paying client and I just need something that works and is easy to maintain, then Zustand. Otherwise Jotai.

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

    Guys, what does bi-directional mean in state managers? Thanks for the video!

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

    awesome

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

    okey your explanation is wonderful but I am curious about something, what if I want to PERSIST my store, how can I do this

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

    Doesn't the supplied "subscribe" api allow for that reactive'esque mobx vibe you mentioned as con? Or am I off base here?

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

      It was tough coming up with a con. ;) Personally I love reactive so it's not a con for me. I just know some folks are super sold on the unidirectional data flow thing and that's its primary interface.

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

    Thx for this great step by step tutorial. What does this bidirectional thing vs unidirectional mean? From what was said in the video, unidirectional seems to mean that the data is mutated only through the exported interface. Am I correct?

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

    Thanks for this video! Would it be possible to do a video on how to combine zustand with SWR or react-query? SWR would be used to fetch the data initially and define the fetch state (loading, error, ...), but zustand is used for further operations on this fetched data. For example similar to your pokemon list example that can be filtered, but in such way that the data request state management is included

    • @jherr
      @jherr  3 ปีที่แล้ว +4

      Maybe it's just me but I'm a little dubious on using fetch wrappers for state management. You could use these two in combination. But for my money I use fetchers to get the data and state managers if we need to shared that data to other places in the app.

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

      @@jherr Fair enough, thanks for your quick response. Do you know if it is possible to fetch and set the initial state from within the zustand hook definition or does it always need to be implemented as a separate 'init' function (similar to the load in your todo list example) followed by calling it where the state hook is used?

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

      @@MaximeGoossens You can do out of React setting of the store, as well as asynchronous actions - github.com/pmndrs/zustand

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

    Thanks for sharing jack, great tutorial 👍, and I would like to ask a question after I have tried with this zustand library:
    When I call console log in TodoList component, I have found that when I call any of the store methods (eg: update, remove ..) which will cause the TopBar component re-rendered?
    Question: is this the limitation of zustand? Or we need to add some memorisation hooks to prevent some unwanted re-renderings?

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

      Use a selector in when you call the Zustand hook to get just the data you need from it. This should reduce the number or re-renders.

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

    Hi Jack,
    I'm a Solutions Architect and WebOps Engineer trying to get back into development. I'm wanting to build a SaaS idea that I have and I'm stuck on choosing a global state manager. I wondered if you could explain the differences between Proxy, Flux and Atomic state? What are the benefits of one over the other? Daishi Kato has solutions for each and I want to be using the right one for my use case.
    Thanks🙏🏼

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

      Feel free to use this as a video topic 😊Just joined the Discord server as well, wish I'd done this before 😅

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

    i began to use zustand recently and got to know about the persist function that comes in the zustand/middleware package, i know very little about persist that it stores the states in local storage,
    but i have come across an issue that the local storage can be altered and misused to change the sensitive data from the browser. I am looking for a solution for this problem. Is there any inbuilt way to prevent anyone from changing local storage data or any in-built feature in zustand that might help with this. Kindly help!

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

      Localstorage is domain specific, so the code would need to be executing within the context of the page. That being said, I would never store anything sensitive in localstorage, regardless of the mechanism I would use to put it there. You should store sensitive data on a service and use authorization and JWTs to access it.

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

    best best best

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

    I still have an error, I think it would be something close to this:
    import create from 'zustand'
    interface LoginState {
    email: string;
    password: string,
    fnOnChangeLogin: (field: string, value: string) => {field: string, value: string}
    }
    export const loginStore = create(set => ({
    fnOnChangeLogin: (obj) => set((state) => ({...state, [obj.field]:obj.value }))
    })

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

    how do slice with typescript

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

      An array slice? Just .slice, same as in JavaScript.

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

      @@jherr I got it, I would like to separate it, you created everything in the store file. I imagine it gets simpler following redux's slice logic

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

    Hi Jack, can we implement zustand in NX mono repos? Thank you

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

      Yes. Maybe I’m missing something but this shouldn’t be an issue. Have you run into one?

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

      @@jherr I tried NX, but not with zustand, I m asking cause I noticed zustand doesn't need

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

      @@farouktouil5036 Yeah, it's really nice that way. It just creates custom hooks that you can use anywhere and get access to the global state.

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

      @@jherrBy the way thank you for the Todo example, I took tour to the documentation of zustand, I let you know that there is actually an update for typescript there ..

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

    lol did you just sike

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

    Having to write the fully Store interface before implementing the store is very annoying. Ideally, Zustand should infer it in some way.

    • @jherr
      @jherr  2 ปีที่แล้ว

      It will if you don’t have value types that change. As in this case names goes from a void to string[].

    • @RodrigoCFD
      @RodrigoCFD 2 ปีที่แล้ว

      @@jherr That's not my experience. Without explicitly typing create(), VSCode highlights _Object is of type 'unknown._ error on set(), and _Parameter 'state' implicitly has an 'any' type_ error on state.

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

      @@RodrigoCFD you are right. Sorry for my error there.

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

    what about Provider?

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

      Zustand doesnt need a provider

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

    Zustand means state in German.

    • @katanaut
      @katanaut 2 ปีที่แล้ว

      Yes, and it’s pronounced as “Tsushtand”