Fireship · @fireship

10 React Antipatterns to Avoid - Code This, Not That!

Uploaded March 1, 2022 · Length 8:55

10 React Antipatterns to Avoid - Code This, Not That!

Summary

10 React antipatterns to avoid, including overly large components, nested components, and performance issues, with tips for refactoring and improving code.

  • react antipatterns
  • react optimization
  • component refactoring
  • performance optimization
  • react best practices

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    reactjs is elegant and minimal on the

  2. 0:02

    surface just write a JavaScript function

  3. 0:04

    to represent a component that

  4. 0:06

    automatically renders when your data

  5. 0:07

    changes it's simple but also misleading

  6. 0:10

    in reality react is a highly complex UI

  7. 0:12

    library with a good amount of technical

  8. 0:14

    baggage that has accumulated over the

  9. 0:16

    last decade many newer Frameworks

  10. 0:17

    attempt to do things better but react is

  11. 0:19

    still the king and if you want to be a

  12. 0:20

    web developer in 2022 it's a very good

  13. 0:23

    idea to learn it the problem is that

  14. 0:24

    react is unopinionated yet provides many

  15. 0:26

    different ways to solve the same

  16. 0:28

    problems that leaves us Developers with

  17. 0:30

    plenty of room to screw things up with

  18. 0:31

    our own stupid ideas in today's episode

  19. 0:34

    of Code This Not That we'll look at 10

  20. 0:36

    anti-patterns in react along with tips

  21. 0:38

    and tricks to improve our code and also

  22. 0:40

    prepare for a front-end technical

  23. 0:41

    interview before we get started I have

  24. 0:43

    an announcement I'm working on a brand

  25. 0:44

    new course for PR members that teaches

  26. 0:46

    the fundamentals of react in my 100

  27. 0:48

    seconds of code format then puts those

  28. 0:50

    Concepts to use by building multiple

  29. 0:52

    full stack applications from scratch the

  30. 0:54

    course is not finished yet but I'll be

  31. 0:55

    releasing new videos for it every couple

  32. 0:57

    days on fireship iio when I initially

  33. 0:59

    started building a react app I usually

  34. 1:01

    start by writing one big giant component

  35. 1:03

    often in the app component provided by

  36. 1:05

    create react app and that's intentional

  37. 1:07

    because I usually don't know how I want

  38. 1:08

    my code to be organized initially and I

  39. 1:10

    don't want to waste my time over

  40. 1:11

    organizing things before I even know

  41. 1:13

    what I want to do however this results

  42. 1:15

    in an anti-pattern of having one overly

  43. 1:17

    large deeply nested component a

  44. 1:19

    component like this is hard to

  45. 1:21

    understand refactor and test we can

  46. 1:22

    improve this code by refactoring into

  47. 1:24

    reusable components like this Navar

  48. 1:26

    could easily be broken down into one or

  49. 1:28

    more components that better represent

  50. 1:30

    what it does now that may sound like a

  51. 1:32

    lot of work but luckily there's a VSS

  52. 1:34

    code extension called glean that can

  53. 1:35

    Leverage The IDE to do this work for us

  54. 1:37

    install it then highlight the code you

  55. 1:39

    want to extract into its own component

  56. 1:41

    click the light bulb and you'll see a

  57. 1:42

    bunch of options to refactor like render

  58. 1:44

    conditionally or extract component give

  59. 1:47

    it a name then it will automatically

  60. 1:48

    move the code while including the

  61. 1:50

    required props it's a massive timesaver

  62. 1:52

    that I couldn't imagine living without

  63. 1:53

    now when you start refactoring a large

  64. 1:55

    component you might be tempted into

  65. 1:56

    another anti- pattern of nesting

  66. 1:58

    components in this example we've defined