Refactoring A Junior’s React Code - 43% Less Code With A Better Data Structure

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ก.ย. 2024

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

  • @MM-pw4ft
    @MM-pw4ft 3 หลายเดือนก่อน +124

    100% do more refactoring videos, these are rare and idk if it's specifically your unique idea to do this, but this is actually gold content

  • @kabeegon
    @kabeegon ปีที่แล้ว +41

    Not gonna lie, this is extremely valuable information for junior developers (and even more experienced ones), thank you for sharing your knowledge, and by the way, your explanations are very clear

  • @Den368
    @Den368 ปีที่แล้ว +83

    Nicely done!
    As a senior developer I can assure that your work here is a gem.

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

      Thanks, that means a lot

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

    I'm a junior dev, and I was wondering why you choose map as the state data structure instead of a set, because you're not keeping the state checked per id, you're only keeping the ones that are checked. And even if you kept all ids and just toggled between true or false, in this context it still makes more sense to me to use a set.

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

      Yes you’re right. Iirc I changed the code to use a set after recording this video. You can check the blog post for that implementation. That’s the advantage of a blog post: you can’t just adjust the video but the blog is easy 🙂

    • @mandarkian
      @mandarkian 3 หลายเดือนก่อน +15

      Mapping is unnecessary all together and you can manage the entirety of this component using a single state. Anyone else also hate the fact that they don’t separate the styling from the implementation of the functional component? You can style elements based on the state of an input in css..

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

    A thought on the data structure you chose. A map works well. Whenever I see a map where the value is a Boolean, I ask myself if a Set would work instead. In this case, I think what you really want is a Set.

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

      Never mind I see you addressed this in another comment. I didn’t see that until now! Thanks

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

    I'm not a junior dev anymore, but thank you for this insightful video.
    Personally, when I am preoccupied with the development or maintenance of a project, I sometimes form some bad habits, like lack of proper error handling in some logical cases, because the project needs to be developed quickly and I slack off while chasing the fast paced development of features. These types of videos really bring me back into reality and make me realize some of my newly acquired deficiencies in React.

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

    Such a well-made video. Kudos to Profy,, this channel deserves way more subscribers and views!

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

      Thanks for the nice words

  • @danieledwards6133
    @danieledwards6133 ปีที่แล้ว +7

    Nice! This was a good example of simplifying the code. I think it would be interesting to see how you might word a review on an example PR like this one.

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

    Absolutely loved this article. I read it last night and could apply it in some work I went back to today, where I'd added unnecessary state that I could've derived

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

      Thanks so much for the feedback!

  • @ra-dro
    @ra-dro ปีที่แล้ว +2

    Thanks for sharing. Cool work.
    One thing I could suggest is to split component into several smaller once.

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

      That's a good suggestion. Honestly I didn't think that much about the JSX. But yeah, especially the table row would be a good candidate for a separate component imo

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

    good video! I would make a habit of saying "simply" less often. what's simple and obvious to you might not be to others!

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

    For the first issue: Pasing an object as a parameter to fill can cause another major issue that all the object have the same reference.
    Here is an example:
    let arr = Array(5).fill({a:1,b:2});
    arr[1].a = 6;
    //This will cause all a to be 6 for "all" the objects in the array

  • @Ss-io9qk
    @Ss-io9qk ปีที่แล้ว +9

    Great stuff! I've definitely been sleeping on the usefulness of maps! Subscribed :)

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

    Very good!
    Yes, I want to see more of this stuff.

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

    What an amazing video for someone who is learning react. Thank you for this!

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

    Juniors code is better than some of the seniors where I work.

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

    I wish I saw this YEARS ago, that was awesome and insightful

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

    excellent i see a lot of general concepts i can apply, especially when implementing that none selected box, thank you

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

    WE NEED MORE OF THIS CONTENT

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

    This is excellent! getting back into React after a long break, and your videos are a godsent. Keep it up!

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

      Thanks :)

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

    Last issue is not using typescript

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

    Thanks. As a just learned react developer, this will help me

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

    Awesome. Would like to see more videos like this

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

    this was amazing and strangely therapeutic

  • @piyushaggarwal5207
    @piyushaggarwal5207 9 หลายเดือนก่อน +75

    Unnecessary state variables are the worst.

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

      As a beginner it's easier to make the mistake... Not a lot of tutorial drive home the concept of derived state well. So as a a beginner you tend to default to patterns you are aware of.

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

      nah, unecessary useEffect is the worst hahaha

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

    Hi, hope you will have more of this video series, or refactoring playlist. Thanks for the content! thumbs up!!

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

      Thanks for the feedback. Check my React Architecture playlist. That's basically a larger refactoring as well only with a focus on architecture

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

    I gotta say.. your tone and presentation is super monotonous,
    but the value of what you say and explain is great. Well done
    I agree with all you said. One thing about the Map though. Personally I dislike using Maps on very big data arrays,
    and simply revert to arrays + writing helper functions for search and filtering but a Map is much more readable and "easier" to use

  • @PraveenKumar-xd7ix
    @PraveenKumar-xd7ix ปีที่แล้ว +1

    Great work brother . Expecting more like this 👍👍

  • @bertrodgers2420
    @bertrodgers2420 ปีที่แล้ว +12

    Hello, as your map of completed issues only ever has a value of true, was the only reason you used a map instead of an array/set here so the lookup could be faster when checking if the issue is completed or not?
    A set of IDs seems to make more sense imo
    Thanks

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

      AFAIK looking up in a map has a constant access time due to it being stored as a hashtable behind the scenes. Meaning it will be the same access time no matte how many elements will be in that list.

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

      ​@@MichaelKireYou are right, but so do sets. Essentially a set is a map mapping to booleans. But I'm not a javascript guy so I also don't know why a map was preferred over a set here.

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

      Scrolled a little more, turns out there is no good reason and he ended up changing to a map and updating the blog post but didn't redo the video (wouldn't expect him to)

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

    Wow this video was actually amazing, that's wild!

  • @rajaj6043
    @rajaj6043 ปีที่แล้ว +6

    Awaiting more video like this, Thanks for posting!!

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

    The refactoring is just on point. Good work.

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

    aah I could watch these all day. Loving it! Very tempted to send my code... hoy could I do that? Cheers!

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

    This Is really helpful as a junior dev myself!

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

    This is was amazing, would love to see more content like this

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

    Do more junior refactor videos!!❤

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

    This is wonderful, this kind of content is very helpful

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

    I love these kinds of videos! Great stuff!

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

    Keep doing these awesome videos. I find them so helpful!

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

    Great video! I would love to see more of these

  • @nelsieb.1366
    @nelsieb.1366 9 หลายเดือนก่อน

    Nice! Would be interesting to see how the solution can be extended for a filterable list

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

    Subscribed! I want more of this. 🤩

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

    Thank you so much. If possible, please upload a video on how to develop a production ready react app, for example the folder structure, custom hooks, global context store, global axios confg, etc.

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

    Great video, really helpful for someone getting started!

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

    thanks for this, helps a lot as a junior

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

    Damn, imagine watching these videos, joining a company as junior dev and writing such code what he wrote after refactoring.

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

    very nice, clear and helpful for me as im practicing with react! although i needed to watch it in 0.75 but thats because im not native english 😅

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

    Awesome stuff, would definitely love to see more of this. Liked 'n subscribed!

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

    please do more refectoring session

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

    Great video Johannes! I had your blog on this lined up for reading for a year now. I saw the video and just went for it now. 😆

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

    Great video!

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

    Loved this kind of video!

  • @yt-sh
    @yt-sh 4 หลายเดือนก่อน

    I subbed, hoping to see more refactoring videos!!

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

    Excellent refactor, well done

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

    wow, great work! how long would it take you to do these refators in real time? I imagine its not done typically within 20 minutes... thanks for the video

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

    I wonder why using a Map instead of a Set to store checkedByIds
    , since it’s just a list of unique strings?

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

      @@bensonding1703 I migrated the blog post to using a set after realizing this. Unfortunately you can’t just change the video that easily 😬 But you’re completely right of course

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

      @@profydev Got it. Thank you! really nice video

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

    I encountered this very same problem (diff example but same premise) and I tried to use a Set with redux toolkit, didn't want to work for some reasonso I had to resort to a dictionary/json where the keys mapped to any value (1, true, null, didn't matter). Then I replaced redux with zustand and it let me... how odd.

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

    array is way faster then a map in this example. only in huge list it would make a difference that's not common to load all on the client side. a small array like this is even faster if you have to loop through it a few times then the overhead of having function for the map. but dont worry a lot of junior devs make that mistake

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

    I would love to see more

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

    Thank you for this video! Helpful 💓💓

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

    Really cool video! Thanks!

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

    Very good. I just don’t agree with ternaries being simpler that if statements. I know frontend developers tend to run from ifs as the devil does from the cross but I think ternaries are a common source of confusion and bugs in lots of front end code.
    Good job

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

      Oh I really don’t like ternaries actually. I try to get around them as much as possible except for really simple conditions. Not sure what I said in that video though 😂

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

    more videos like this please!

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

    Amazing video ! May I ask when is it appropriate to use a map vs an array?

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

    thank you very much for the video

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

    Great video :)

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

    This was very insightful. As a junior dev I learned a lot. Most importantly is that I really have to start using the new Map instead of just objects and I actually didn't know about the ".size" property. does it work with "regular" objects?
    Last question is a bit longer but since you touched on that subject I'll allow myself to ask that:
    I have an app in which I have a sidebar with buttons which run functions (ofc) and one of those uses the text in the main layout. In that case, using a ref felt like bad practice since I would have to either forward it through many cmps, store it in the global store or use context instead of just accessing it directly... what is your take on that? Thanks! :)

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

      Thanks a lot for the feedback. You only have the size on Map and Set not on regular objects.
      Regarding the shared text: not sure but this might be a good case for using context. Maybe there's also an alternative way of structuring the app that's more efficient. Hard to say. If you want me to do a similar review feel free to send me the code at review@profy.dev

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

      @@profydev Thanks for the comment (I subbed by the way, keep it up, I know many people are looking for content exactly like that).
      I wish I could, that would be very useful. but it's quite a big app which I wrote for work and it's on a closed network :( Can I email you the question with more context anyway?

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

      @@Tom_Rose Sure, feel free to send me an email to johannes@profy.dev. There's no need to share the whole code btw. A reduced version on CodeSandbox would even be better

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

    As a senior swe, one thing I would nitpick is this awful double negation (!!). I would propose changing it to the Boolean constructor and adding a custom eslint rule disallowing UnaryExpression (!) as an argument of UnaryExpression (!)

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

      Eslint rule is way too overkill but I do agree on the double negation😊

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

    has-method fits here better 5:45

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

      Thanks, good catch. It's even better to replace the Map with a Set (suggestion from someone on Reddit). I adjusted the code in the blog post (see the description). But unfortunately, I can't update the video anymore

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

    Instead of using a Map maybe just have an array of stateId's and use indexOf or includes instead of get on the Map its both faster and simpler because the truthiness is defined if it exist and dosn't carry a true value as well so its both faster and uses less memory

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

    Do it just do it this is awesome

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

    I understand the convention that methods returning booleans should begin with `is`, since the assumption is that the method's object is the noun part of the clause. However, free variables are also nouns, not actions. I cannot help but feel that this convention being carried over to variables is a consequence of dogma, rather than usefulness. "IsIssueOpen" is a question, not a statement. "If is issue open" is not good English.
    Does anyone have an argument to counter this?

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

    wow, more of this

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

    im confused about the usage of !!, i understand it does a double negation and converts to boolen, but why concert to boolean if the value is falsy anyway?

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

      More than that, couldn't he just have used Map.has() instead of double negating Map.get()?

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

    One question , the thing where you replace the for loop for counting elements that have the status open with a filtered array ( arr.filter().length ) just to have it just "pretty" impact the memory usage ? U don't just iterate thought the the array that you have , u create a new array so , allocating more memory .Maybe a middle ground can be a reducer :D

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

      Nvm , I didn't get to the part where you mentioned about memory :D , great job XD

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

      Haha thanks

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

    Can you add a live clock extension to vsCode so we can judge how long the rafactor actually took otherwise imposter syndrome starts acting up with all the time skips :D

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

      Haha that wouldn't help either as this was for sure not the first time I refactored this code :)

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

    Can you point me to resources explaining the !! on the input's disabled attribute?

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

      The double exclamation operator is to forcibly cast a boolean value in a variable.
      let something = "Test";
      console.log(typeof something); // string
      let otherthing = !!something;
      console.log(typeof otherthing); // boolean
      So basically, the !! in the disabled attribute forces the assigned variable to act as a boolean value. It's for type safety.

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

    Omg u do it all so fast compared to me, it makes me cry🥺😭
    But i wont give up 💪

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

      This video is heavily edited. So all the thinking breaks are cut out and the typing is sped up. No worries, you're probably not too slow :) If you want to watch me in real time check out this playlist. Takes me 10 hours to build a freaking sidebar navigation (plus a lot of setup and stuff but still): th-cam.com/play/PLo6qcHP9e9W5T0cwCWsQ4qcoXATqvMzcS.html

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

      @@profydev i watch it in half speed haha, thanks!
      Good communication, u are awesome

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

    2:45 - I disagree about using refs to access the dom in 2024. Ref access was Reacts way of hiding their performance issues back in the day. But dom access is fine now. 10 years later good grief.

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

    Very helpful review. Thanks for sharing
    Although I'm still not able to understand one thing
    In the first line of handleOnChange method you doing this const updatedCheckedById = new Set(checkedById); (I have taken this from your blogpost , in the video it is Map)
    How come this update the previous Set and if so the next if check should return true, but surprisingly it is returning false
    Can you share some thought here
    Apologise for my lack of knowledge in advanced

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

      I'm not sure if I get the problem right. But let me try to explain:
      1. Initially checkedById is an empty set
      2. new Set(checkedById) just clones the current set. The goal is to create a new reference so we can use it to update the state (as React state has to be immutable)
      3. The first if uses updatedCheckedById.has(id). Since checkedById is initially empty so is updatedCheckedById. So .has(id) returns false.
      4. If an item is already selected its id is included in the set. Then .has(id) returns true and the id is removed from the set. The result is that the item is unselected.
      Does that make sense?

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

    Can i use Map, Set, Proxy, inside a useState Hook?

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

      You can definitely use Map and Set with useState. Proxy I'm not sure

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

    Boss level♥

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

    How did he delete the whole line at once??

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

      Probably ctrl x without selecting anything. Then the current line should be gone.

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

      You people need to use the vim extension. If you think deleting a single line is impressive, vim will blow your fkn mind

  • @МартинИванов-в8ы
    @МартинИванов-в8ы ปีที่แล้ว +1

    Could have used a set

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

      Thanks for the feedback. True, somebody on Reddit pointed out the same thing. The blog post that's linked in the description now uses a Set. Unfortunately I can't update the video here.

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

    Hello MrBeast!

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

    10:07 idk much about react, but readability was easier. Now Id make a comment above about the false states.

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

    Interesting

  • @Hacking-Kitten
    @Hacking-Kitten 3 หลายเดือนก่อน

    bitflag, where?

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

    why doesn't this guy blink

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

    Very cool content but a bit too fast IMHO. 👍

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

    I feel bad I just don’t understand

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

    Mmmore of. This please

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

    Those are some long variable and function names lol

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

    Wow Sar, how to become pero laik u

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

    WTF is with the bot comments?

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

    Love it, thanks from sharing.

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

      Thanks for watching!