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

  Рет қаралды 64,711

Profy dev

Profy dev

Күн бұрын

Find the blog post to this video including the complete code here:
profy.dev/article/react-junio...
Find a detailed guide to debugging React with VS Code here: profy.dev/article/debug-react...
00:00 - Intro
00:08 - Demo of the app
00:35 - Code Review: Overview
01:28 - Code Review: Data Structure
02:18 - Code Review: Unnecessary State
02:43 - Code Review: Directly Accessing The DOM
03:03 - Refactor: Data Structure
07:26 - Refactor: Unnecessary State
08:52 - Refactor: Remove document.getElementById()
10:29 - Refactor: Final Clean Up
11:36 - Final Review & Summary

Пікірлер: 114
@MM-pw4ft
@MM-pw4ft 7 күн бұрын
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
@diogoribeiro477
@diogoribeiro477 16 күн бұрын
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 16 күн бұрын
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 🙂
@ogmandarkian
@ogmandarkian 11 күн бұрын
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..
@ploonkey
@ploonkey Жыл бұрын
This is great, would love to see more of these.
@Ss-io9qk
@Ss-io9qk Жыл бұрын
Great stuff! I've definitely been sleeping on the usefulness of maps! Subscribed :)
@piyushaggarwal5207
@piyushaggarwal5207 6 ай бұрын
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. 😆
@skapha
@skapha Жыл бұрын
Very good! Yes, I want to see more of this stuff.
@gilbertmutai3342
@gilbertmutai3342 7 ай бұрын
The refactoring is just on point. Good work.
@danieledwards6133
@danieledwards6133 Жыл бұрын
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.
@tayshin.gg.
@tayshin.gg. Жыл бұрын
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
@kopfmann45
@kopfmann45 Ай бұрын
This is excellent! getting back into React after a long break, and your videos are a godsent. Keep it up!
@profydev
@profydev 26 күн бұрын
Thanks :)
@amantulsyan1082
@amantulsyan1082 Жыл бұрын
This is was amazing, would love to see more content like this
@PraveenKumar-xd7ix
@PraveenKumar-xd7ix Жыл бұрын
Great work brother . Expecting more like this 👍👍
@mohammedsamir5142
@mohammedsamir5142 22 күн бұрын
Keep doing these awesome videos. I find them so helpful!
@Den368
@Den368 Жыл бұрын
Nicely done! As a senior developer I can assure that your work here is a gem.
@profydev
@profydev Жыл бұрын
Thanks, that means a lot
@franciscoizaguirre9069
@franciscoizaguirre9069 7 күн бұрын
I wish I saw this YEARS ago, that was awesome and insightful
@irondsd
@irondsd Жыл бұрын
Awesome. Would like to see more videos like this
@miceyjuice9175
@miceyjuice9175 13 күн бұрын
I love these kinds of videos! Great stuff!
@JulietteReacts
@JulietteReacts 10 ай бұрын
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 10 ай бұрын
Thanks so much for the feedback!
@Gelu84
@Gelu84 13 күн бұрын
Great video! I would love to see more of these
15 күн бұрын
Great video, really helpful for someone getting started!
@kewtomrao
@kewtomrao Жыл бұрын
This Is really helpful as a junior dev myself!
@rajaj6043
@rajaj6043 Жыл бұрын
Awaiting more video like this, Thanks for posting!!
@NoopDawg
@NoopDawg 13 күн бұрын
This is wonderful, this kind of content is very helpful
@barhoumiahmed179
@barhoumiahmed179 14 күн бұрын
WE NEED MORE OF THIS CONTENT
@tellyisko6908
@tellyisko6908 Жыл бұрын
Subscribed! I want more of this. 🤩
@okage_
@okage_ 15 күн бұрын
thanks for this, helps a lot as a junior
@BCRooke1
@BCRooke1 2 күн бұрын
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 2 күн бұрын
Never mind I see you addressed this in another comment. I didn’t see that until now! Thanks
@TommasoManca
@TommasoManca 25 күн бұрын
Awesome stuff, would definitely love to see more of this. Liked 'n subscribed!
@samareshdas767
@samareshdas767 Жыл бұрын
Thanks. As a just learned react developer, this will help me
@DomskiPlays
@DomskiPlays 9 күн бұрын
Wow this video was actually amazing, that's wild!
@dyunior
@dyunior 27 күн бұрын
Hi, hope you will have more of this video series, or refactoring playlist. Thanks for the content! thumbs up!!
@profydev
@profydev 26 күн бұрын
Thanks for the feedback. Check my React Architecture playlist. That's basically a larger refactoring as well only with a focus on architecture
@Stragunafen
@Stragunafen 11 күн бұрын
Loved this kind of video!
@developer_george
@developer_george Жыл бұрын
Thank you for this video! Helpful 💓💓
@psatler
@psatler Жыл бұрын
Really cool video! Thanks!
@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.
@yt-sh
@yt-sh 13 күн бұрын
I subbed, hoping to see more refactoring videos!!
@tonienguix4834
@tonienguix4834 Жыл бұрын
aah I could watch these all day. Loving it! Very tempted to send my code... hoy could I do that? Cheers!
@ToadyEN
@ToadyEN Жыл бұрын
Juniors code is better than some of the seniors where I work.
@ra-dro
@ra-dro Жыл бұрын
Thanks for sharing. Cool work. One thing I could suggest is to split component into several smaller once.
@profydev
@profydev Жыл бұрын
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
@idoschacham6276
@idoschacham6276 Жыл бұрын
Excellent refactor, well done
@bao3576
@bao3576 15 күн бұрын
Very informative!
@blaycoder
@blaycoder 27 күн бұрын
I would love to see more
@stephaniemay9090
@stephaniemay9090 15 күн бұрын
more videos like this please!
@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 😅
@nelsieb.1366
@nelsieb.1366 6 ай бұрын
Nice! Would be interesting to see how the solution can be extended for a filterable list
@Famouzi
@Famouzi Жыл бұрын
Great video :)
@mrlectus
@mrlectus 6 күн бұрын
Last issue is not using typescript
@a7mdbest15
@a7mdbest15 13 күн бұрын
thank you very much for the video
@fabrzy3784
@fabrzy3784 12 күн бұрын
Amazing video ! May I ask when is it appropriate to use a map vs an array?
@bertrodgers2420
@bertrodgers2420 Жыл бұрын
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 14 күн бұрын
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 14 күн бұрын
​@@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 14 күн бұрын
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)
@itsMapleLeaf
@itsMapleLeaf Жыл бұрын
good video! I would make a habit of saying "simply" less often. what's simple and obvious to you might not be to others!
@kimballle7714
@kimballle7714 Жыл бұрын
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
@sarcasticdna
@sarcasticdna Жыл бұрын
Do it just do it this is awesome
@BlackxesWasTaken
@BlackxesWasTaken Ай бұрын
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
@Tom_Rose
@Tom_Rose Жыл бұрын
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
@d_3677
@d_3677 Жыл бұрын
Boss level♥
@piyushaggarwal5207
@piyushaggarwal5207 6 ай бұрын
Unnecessary state variables are the worst.
@moveonvillain1080
@moveonvillain1080 15 күн бұрын
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.
@SpikeYOLO994
@SpikeYOLO994 12 күн бұрын
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 12 күн бұрын
Nvm , I didn't get to the part where you mentioned about memory :D , great job XD
@profydev
@profydev 11 күн бұрын
Haha thanks
@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
@flalspspsl6858
@flalspspsl6858 14 күн бұрын
a note on Map and Set Javascript structures. with Node.js it's fine to use them, but avoid using them on v8 (browser). V8 uses a ton of optimization techniques, specifically for basic array and objects that cannot be done for Map and Set's. to be even more specific, Map and Set cannot be cached as effectively and thus become slower than if it was otherwise implemented with a basic object or array. I'm not 100% sure if this is still the case but it was when this video came out.
@profydev
@profydev 13 күн бұрын
Thanks for the hint
@tyu3456
@tyu3456 9 күн бұрын
Doesn't Node.js also use V8?
@grenadier4702
@grenadier4702 Жыл бұрын
has-method fits here better 5:45
@profydev
@profydev Жыл бұрын
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
@Seth_H_Builds
@Seth_H_Builds Жыл бұрын
Can you point me to resources explaining the !! on the input's disabled attribute?
@sjn_
@sjn_ Жыл бұрын
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.
@ploonkey
@ploonkey Жыл бұрын
Also you can ignore the !!isIssueOpen at 11:24 since the variable was already a bool., he just forgot to remove it.
@LaFragas
@LaFragas Жыл бұрын
Omg u do it all so fast compared to me, it makes me cry🥺😭 But i wont give up 💪
@profydev
@profydev Жыл бұрын
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): kzbin.info/aero/PLo6qcHP9e9W5T0cwCWsQ4qcoXATqvMzcS
@LaFragas
@LaFragas Жыл бұрын
@@profydev i watch it in half speed haha, thanks! Good communication, u are awesome
@PetersExcapades
@PetersExcapades 6 күн бұрын
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 5 күн бұрын
More than that, couldn't he just have used Map.has() instead of double negating Map.get()?
@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?
@reikromulis7747
@reikromulis7747 10 ай бұрын
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 10 ай бұрын
Haha that wouldn't help either as this was for sure not the first time I refactored this code :)
@samarrowsmith2723
@samarrowsmith2723 15 күн бұрын
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?
@Deutschehordenelite
@Deutschehordenelite 15 күн бұрын
10:07 idk much about react, but readability was easier. Now Id make a comment above about the false states.
@jayeshcholker
@jayeshcholker 14 күн бұрын
How did he delete the whole line at once??
@profydev
@profydev 13 күн бұрын
Probably ctrl x without selecting anything. Then the current line should be gone.
@SogMosee
@SogMosee 12 күн бұрын
You people need to use the vim extension. If you think deleting a single line is impressive, vim will blow your fkn mind
@xav_624
@xav_624 Жыл бұрын
Very cool content but a bit too fast IMHO. 👍
@user-uo3ox5hf6u
@user-uo3ox5hf6u Жыл бұрын
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.
@Hacking-Kitten
@Hacking-Kitten 9 күн бұрын
bitflag, where?
@mateito191
@mateito191 12 күн бұрын
I feel bad I just don’t understand
@cyrusren7260
@cyrusren7260 Жыл бұрын
Wow Sar, how to become pero laik u
@sjn_
@sjn_ Жыл бұрын
Those are some long variable and function names lol
@nested9301
@nested9301 13 күн бұрын
Mmmore of. This please
@jeremykothe2847
@jeremykothe2847 8 күн бұрын
WTF is with the bot comments?
@meaningmean
@meaningmean 9 ай бұрын
Love it, thanks from sharing.
@profydev
@profydev 8 ай бұрын
Thanks for watching!
Turning bad React code into senior React code
13:10
Cosden Solutions
Рет қаралды 84 М.
1❤️
00:20
すしらーめん《りく》
Рет қаралды 33 МЛН
Can you beat this impossible game?
00:13
LOL
Рет қаралды 51 МЛН
Cute Barbie Gadget 🥰 #gadgets
01:00
FLIP FLOP Hacks
Рет қаралды 35 МЛН
ONE MORE SUBSCRIBER FOR 6 MILLION!
00:38
Horror Skunx
Рет қаралды 15 МЛН
The Story of React Query
8:55
uidotdev
Рет қаралды 64 М.
What does larger scale software development look like?
24:15
Web Dev Cody
Рет қаралды 1,2 МЛН
Why I Like Programming in C.
3:16
Francisco Jinto Fox
Рет қаралды 18 М.
Signals. I spent 2 years to understand this part.
21:24
kimylamp
Рет қаралды 163 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 150 М.
Harder Than It Seems? 5 Minute Timer in C++
20:10
The Cherno
Рет қаралды 103 М.
My Brain after 569 Leetcode Problems
7:50
NeetCode
Рет қаралды 2,4 МЛН
Understanding B-Trees: The Data Structure Behind Modern Databases
12:39
How Senior Programmers ACTUALLY Write Code
13:37
Thriving Technologist
Рет қаралды 1,3 МЛН
All useEffect Mistakes Every Junior React Developer Makes
22:23
1❤️
00:20
すしらーめん《りく》
Рет қаралды 33 МЛН