Prerequisites

  • Install the React DevTools
    (Chrome
    (recommended),
    Firefox)
  • Experience with React and all hooks

System Requirements

All of these must be available in your PATH. To verify things are set up
properly, you can run this:

git --version
node --version
npm --version

If you have trouble with any of these, learn more about the PATH environment
variable and how to fix it here for [windows][win-path] or
[mac/linux][mac-path].

Setup

After you've made sure to have the correct things (and versions) installed, you
should be able to just run a few commands to get set up:

git clone https://github.com/kentcdodds/react-performance.git
cd react-performance
npm run setup --silent

This may take a few minutes. It will ask you for your email. This is
optional and just automatically adds your email to the links in the project to
make filling out some forms easier.

If you get any errors, please read through them and see if you can find out what
the problem is. If you can't work it out on your own then please [file an
issue][issue] and provide all the output from the commands you ran (even if
it's a lot).

Running the app

To get the app up and running (and really see if it worked), run:

npm start

This should start up your browser. If you're familiar, this is a standard
react-scripts application.

You can also open
the deployment of the app on Netlify.

Running the tests

npm test

This will start Jest in watch mode. Read the output and
play around with it. The tests are there to help you reach the final version,
however sometimes you can accomplish the task and the tests still fail if you
implement things differently than I do in my solution, so don't look to them as
a complete authority.

Exercises

  • src/exercise/00.md: Background, Exercise Instructions, Extra Credit
  • src/exercise/00.js: Exercise with Emoji helpers
  • src/__tests__/00.js: Tests
  • src/final/00.js: Final version
  • src/final/00.extra-0.js: Final version of extra credit

The purpose of the exercise is not for you to work through all the material.
It's intended to get your brain thinking about the right questions to ask me as
I walk through the material.

Helpful Emoji 🐨 💪 🏁 💰 💯 🦉 📜 💣 👨‍💼 🚨

Each exercise has comments in it to help you get through the exercise. These fun
emoji characters are here to help you.

  • Kody the Koala 🐨 will tell you when there's something specific you should
    do
  • Matthew the Muscle 💪 will indicate what you're working with an exercise
  • Chuck the Checkered Flag 🏁 will indicate that you're working with a final
    version
  • Marty the Money Bag 💰 will give you specific tips (and sometimes code)
    along the way
  • Hannah the Hundred 💯 will give you extra challenges you can do if you
    finish the exercises early.
  • Olivia the Owl 🦉 will give you useful tidbits/best practice notes and a
    link for elaboration and feedback.
  • Dominic the Document 📜 will give you links to useful documentation
  • Berry the Bomb 💣 will be hanging around anywhere you need to blow stuff
    up (delete code)
  • Peter the Product Manager 👨‍💼 helps us know what our users want
  • Alfred the Alert 🚨 will occasionally show up in the test failures with
    potential explanations for why the tests are failing.

Workshop Feedback

Each exercise has an Elaboration and Feedback link. Please fill that out after
the exercise and instruction.

At the end of the workshop, please go to this URL to give overall feedback.
Thank you! https://kcd.im/rp-ws-feedback

Transcript

Kent C. Dodds: 0:01 React itself is pretty fast. If you take a React application and then you try to build that same application in Vanilla JS, you're probably going to have to build a bunch of abstractions. Honestly, the abstraction you build is unlikely to be as fast as the abstraction that we have, which is React.

0:17 Sometimes, there are some things that we have to do that are going to be slow. To help with some of those things, React gives us this toolbox of tools that we can reach for when React is not optimizing us quite to the level that we need it to.

0:35 Maybe, you're building a React Native app and there's this interaction, where every single re-render counts and you want to avoid unnecessary re-renders there, or you are rendering a huge list of data, or you've got some data grid that you're showing whatever the case may be there, or some situations where React built in optimizations isn't quite enough for you.

0:54 That is what React Performance is all about. Without being able to identify which tool of those tools in the tool chest is appropriate, you might actually apply the wrong one in the wrong situation. You wouldn't want to grab a hammer to screw in a screw. That's not going to work very well.

1:12 Lots of React Performance is just showing you how to identify what performance problems there are, and which tools are best to apply to solve those performance problems. I'm looking forward to you exploring that and learning all about that.

1:26 Let's get you set up. Here's the React Performance Repo. It's just React Performance. You come down here to the setup. You copy and paste that in your terminal, get yourself all set up. Inside of here, we have the exercises, but we also have the examples directory.

1:41 There are a couple of examples that will be useful to you in here, to get an idea of how a different files work together, when we're doing lazy loading or code splitting, lazy loading of this code. You'll be playing around in there a little bit, but most of your time will be spent in these exercises.

1:57 Actually, lots of these exercises don't require a lot of code changes. What you'll actually be doing is spending a lot of your time in the Network tab, the Profiler, and the Performance tab. You may or may not be very familiar with these tools and that's totally fine. I don't expect any proficiency in these tools, but I do expect you to spend a fair amount of time playing around with these tools.

2:23 It would actually also be awesome for you to open up these same tools in the app that you're working on at work or your side project, so that you can get an idea of what these tools look like in a real application.

2:36 Like I said, there's not a whole lot of code changes, you're just reaching for the tool that React is giving you, and you run off with it to make things faster. These are the tools that you're going to be using. The Performance tab, the Profiler, and the Network tab.

2:50 These are the tools that you use to identify what problem you're facing, so that you know which tool to reach for. Don't be afraid, especially this Performance tab. I know it can be a little bit daunting, but just play around with it.

3:03 You're not going to break anything. Just run around, see what is going on, and the more time you spend in this tool, the more experience you're going to gain in it. It will seem more second nature to you after a while. You're going to spend a lot of time in there.

3:17 We're going to be learning about code-splitting, the useMemo hook for expensive calculations, React.memo(), that one's pretty fun to avoid unnecessary re-renders, and using react-virtual to avoid rendering large lists of stuff, because if you render 100,000 items, it's going to be slow, or 300,000. You're not going to do that, and especially in a mobile browser.

3:48 React-virtual is really handy tool for us. Then we're also going to talk about optimizing the context value, performance death by 1,000 cuts. Sometimes there's not one thing that's the problem, it's just that you have tons of things that are consuming this value that need to change every time this value changes.

4:06 I'm going to show you how to optimize for that. Some tools that React gives you, as well as some libraries that you can use to help with some of those problems. Then production performance monitoring, which I think you'll find it's interesting and can hopefully help you avoid regressions when you ship to production.

4:25 I think that this is all a lot of fun. I think that you'll have a good time with it. Again, spend a lot of time in the DevTools, because that's where you're going to be able to identify which of the React tools you need to reach for your optimizations.

4:39 Again, React is really fast, but sometimes there are some things you need to do with it or you need to give it a little bit of extra information and some extra help to avoid running as much code. Have a good time with this and we'll see you on the other side of the workshop.