Fireship · @fireship
10 React Antipatterns to Avoid - Code This, Not That!
Uploaded March 1, 2022 · Length 8:55
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.
- 0:00
reactjs is elegant and minimal on the
- 0:02
surface just write a JavaScript function
- 0:04
to represent a component that
- 0:06
automatically renders when your data
- 0:07
changes it's simple but also misleading
- 0:10
in reality react is a highly complex UI
- 0:12
library with a good amount of technical
- 0:14
baggage that has accumulated over the
- 0:16
last decade many newer Frameworks
- 0:17
attempt to do things better but react is
- 0:19
still the king and if you want to be a
- 0:20
web developer in 2022 it's a very good
- 0:23
idea to learn it the problem is that
- 0:24
react is unopinionated yet provides many
- 0:26
different ways to solve the same
- 0:28
problems that leaves us Developers with
- 0:30
plenty of room to screw things up with
- 0:31
our own stupid ideas in today's episode
- 0:34
of Code This Not That we'll look at 10
- 0:36
anti-patterns in react along with tips
- 0:38
and tricks to improve our code and also
- 0:40
prepare for a front-end technical
- 0:41
interview before we get started I have
- 0:43
an announcement I'm working on a brand
- 0:44
new course for PR members that teaches
- 0:46
the fundamentals of react in my 100
- 0:48
seconds of code format then puts those
- 0:50
Concepts to use by building multiple
- 0:52
full stack applications from scratch the
- 0:54
course is not finished yet but I'll be
- 0:55
releasing new videos for it every couple
- 0:57
days on fireship iio when I initially
- 0:59
started building a react app I usually
- 1:01
start by writing one big giant component
- 1:03
often in the app component provided by
- 1:05
create react app and that's intentional
- 1:07
because I usually don't know how I want
- 1:08
my code to be organized initially and I
- 1:10
don't want to waste my time over
- 1:11
organizing things before I even know
- 1:13
what I want to do however this results
- 1:15
in an anti-pattern of having one overly
- 1:17
large deeply nested component a
- 1:19
component like this is hard to
- 1:21
understand refactor and test we can
- 1:22
improve this code by refactoring into
- 1:24
reusable components like this Navar
- 1:26
could easily be broken down into one or
- 1:28
more components that better represent
- 1:30
what it does now that may sound like a
- 1:32
lot of work but luckily there's a VSS
- 1:34
code extension called glean that can
- 1:35
Leverage The IDE to do this work for us
- 1:37
install it then highlight the code you
- 1:39
want to extract into its own component
- 1:41
click the light bulb and you'll see a
- 1:42
bunch of options to refactor like render
- 1:44
conditionally or extract component give
- 1:47
it a name then it will automatically
- 1:48
move the code while including the
- 1:50
required props it's a massive timesaver
- 1:52
that I couldn't imagine living without
- 1:53
now when you start refactoring a large
- 1:55
component you might be tempted into
- 1:56
another anti- pattern of nesting
- 1:58
components in this example we've defined


