DEV Community

Cover image for The React Cheatsheet for 2020 📄 (+ Real-World Examples)

The React Cheatsheet for 2020 📄 (+ Real-World Examples)

Reed Barger on January 19, 2020

I've put together for you an entire visual cheatsheet of all of the concepts and skills you need to master React in 2020. But don't let the label ...
Collapse
 
ulubayam profile image
Gizem Ulubayam

Hi, great article. Thank you for your effort. You say list and keys section "use array element index for key" but this is not a good idea. Because when you use an index of an array as a key, React will optimize and not render as expected and also it is an anti-pattern. I just wanted to specify. Have a nice day.

Collapse
 
oziniak profile image
Oleh Ziniak • Edited

As long as you do not reorder or delete any items, indexes are ok. It's ok even if you add items to the end, or always delete the last one.

Collapse
 
ulubayam profile image
Gizem Ulubayam

Yes, you are right. It can only be missed on a large-scale project. So I wanted to specify.

Collapse
 
reedbarger profile image
Reed Barger • Edited

I mention that a unique id is better.

Collapse
 
ulubayam profile image
Gizem Ulubayam • Edited

Ok. I must have missed. Thank you.

Collapse
 
allpro profile image
Kevin Dalman

You put a lot of work into this. Kudos. However this description misses a key point...

If you have a component that re-renders frequently, useCallback prevents callback functions within the component from being recreated every single time the component re-renders (which mean the function component re-runs)

The main goal of useCallback is so child components won't re-render unnecessarily. If you create a new function on every render, and pass it as a 'callback' to a child component, that child will always re-render because it receives a different prop each time. This is why the hook is named use-callback.

Collapse
 
reedbarger profile image
Reed Barger

Child components are not the only time when you will use useCallback. This applies to local functions used within the same component. For example, those used in the useEffect hook.

This is evident if you are using a local function within useEffect and don't include it within the dependencies array. If you're using the eslint plugin for React hooks, you'll get a linting warning which tells you to: 'move the function inside the effect, or wrap it with useCallback.'

I'm highlighting how hooks are used in practice based off of experience, not merely quoting the documentation.

Collapse
 
allpro profile image
Kevin Dalman

Child components are not the only time when you will use useCallback.

True, but it is a primary use for the hook, which was not mentioned at all, which is why I pointed it out.

This is evident if you are using a local function within useEffect and don't include it within the dependencies array. If you're using the eslint plugin for React hooks, you'll get a linting warning.

The useCallback hook is memoization. Overusing memoization can actually slow code because it's more time-consuming to process than to regenerate simple functions. It often adds complexity with no meaningful benefit.

The scenario you describe should occur only rarely in good code. It is more often a sign of premature optimization when every function is wrapped in useCallback. But how to decide when to or not to? For a good primer I suggest:

kentcdodds.com/blog/usememo-and-us...

I'm highlighting how hooks are used in practice based off of experience, not merely quoting the documentation.

Again, Kudos for taking time to share. However it's good to also grok why documented recommendations are what they are.

I'm a project lead with 30 years experience. I train and mentor devs daily on JavaScript, React, and all things code. I still try to learn something new every day though.

Thread Thread
 
reedbarger profile image
Reed Barger • Edited

I'm not going to have a discussion about what you think should be emphasized. Everything I mentioned is correct. This is just a quick cheatsheet. They're not supposed to include everything.

Think about others before you comment, please. If you have a suggestion for me, fine. You can send me a message directly and we can talk about it, but there's no need to clutter up what is a cheatsheet for beginners with multiple paragraphs worth of opinions about how the article should be written.

Thread Thread
 
tangobravo profile image
tangobravo

Hi Reed,

Thanks very much for the article (and entire series), it's a great hooks-first treatment for learning React now. I'm just about to embark on my first React project but have pretty extensive experience in other languages and all the way down to the metal in terms of assembly coding. React is quite a different paradigm to what I'm used to but I'm really interested and excited by it.

The useCallback description did have me scratching my head - I really couldn't see how the performance hit of defining (not calling) an inline function would be that bad in your example when that component re-renders - the rest of the code uses arrow functions a lot which is basically short-hand for the same thing after all.

Kevin's comment did really clear things up for me - I can absolutely see a performance issue when wanting to pass on a callback as a prop to a child component, and how useCallback can therefore prevent those children from re-rendering. The name also makes more sense in that context.

I don't want to add to unnecessary clutter but wanted to add my perspective as someone with coding experience but new to React, and thank Kevin for clearing up the one aspect that was confusing me in the post.

Collapse
 
karfau profile image
Christian Bewernitz

Looks like a very welcoming and nicely done Series to get started! (Just found out about it.)

In case somebody is wondering how some things work with react and typescript:

GitHub logo typescript-cheatsheets / react

Cheatsheets for experienced React developers getting started with TypeScript

React+TypeScript Cheatsheets

react + ts logo

Cheatsheets for experienced React developers getting started with TypeScript

Web docs | 中文翻译 | Español | Contribute! | Ask!



👋 This repo is maintained by @swyx, @ferdaber, @eps1lon, @jsjoeio and @arvindcheenu, we're so happy you want to try out TypeScript with React! If you see anything wrong or missing, please file an issue! 👍


All Contributors | Discord | Tweet

All React + TypeScript Cheatsheets

  • The Basic Cheatsheet (/README.md) is focused on helping React devs just start using TS in React apps
    • Focus on opinionated best practices, copy+pastable examples.
    • Explains some basic TS types usage and setup along the way.
    • Answers the most Frequently Asked Questions.
    • Does not cover generic type logic in detail. Instead we prefer to teach simple troubleshooting techniques for newbies.
    • The goal is to get effective with TS without learning too much TS.
  • The Advanced Cheatsheet (/ADVANCED.md) helps show…






is a well maintained reference point.
Collapse
 
madza profile image
Madza • Edited

Finally a proper contender to take down Simon Holdorf from the Infinity Top's throne ;)

Collapse
 
dvdvdmt profile image
Dmitriy Davydov

Hi Reed, thank you for the cheatsheet. I found an error in Mouse Tracker example.
This code will not remove mousemove handler:

window.removeEventListener("mousemove", event => {
        const { pageX, pageY } = event;
        setMousePosition({ x: pageX, y: pageY });
      });

Here is a life example. After you press 'Toggle mouse tracker' you'll see an error in console. It happens because you should provide a reference to the handler that you want unsubscribe from. So here is the valid example.

Collapse
 
reedbarger profile image
Reed Barger

You're right, we need a reference to the same function. That was my original code, but I removed the reference mistakenly because I thought the abstraction of the callback to handleMouseMove was unneeded. It's fixed now. Thanks Dmitriy!

Collapse
 
tangobravo profile image
tangobravo • Edited

There's still a comment at the end of that example that dates from the original version too.

Collapse
 
momin profile image
Md Abdul Momin

Both dev.to and your Blogs these line is okay return <div>I am learning {language}</div>; but pdf cheatsheet should update, because of closing tags!

Collapse
 
bhupendra1011 profile image
bhupendra

awesome pocket guide - react cheat-sheet. gearing up for react journey :-)

Collapse
 
esandez profile image
esandez

Nice post! I've shared ot with my colleagures who are still beginners in React.

However, I would like to say that the PDF is missing something really important: Syntax highlighting (as the post itself has). It's kind of exhausting looking at pieces of code of the same color and the less experienced people would thank having it.

Collapse
 
reedbarger profile image
Reed Barger

Hi Eric,

Thank you!

I update the cheatsheets I make to include syntax highlighting. I usually have to do that by hand. I like to use plain text to make sure that the code is still copyable. If I used snapshots, you would have syntax highlighting, but would have to rewrite the code yourself.

Thanks for the feedback, just wanted to provide more info in this regard.

Best,
Reed

Collapse
 
glinkot profile image
glinkot

Super useful. Couple of items that would be nice to include:

  • The fact custom hooks have to start with 'use'. I was scratching my head a couple of days ago wondering how they differed from other functions
  • Some examples of passing arguments up through events/handlers in subcomponents. How those should be names is something I'm still working out and would be great in a cheatsheet.

Thanks again!

Collapse
 
momin profile image
Md Abdul Momin

If I found something subject-oriented with real-life examples and of course in a nutshell, that's will be a react cheatsheet you have been provided, moreover if I did not subscribe to Bob Ziroll Bootcamp at SCRIMBA sure I will signup for your react Bootcamp.

Collapse
 
immyjosh profile image
Immanuel Joshua Paul

How do i make my react app SEO friendly ? As I've completed my front end site using create-react-app . Please help !!

Collapse
 
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

In terms of meta descriptions, take a look at react-helmet.

Collapse
 
mateiadrielrafael profile image
Matei Adriel

Unpopular opinion: react-helmet is just a glorified built in portal

Collapse
 
httpspauline profile image
Pauline

Amazing, thanks for putting this together!

Collapse
 
reedbarger profile image
Reed Barger

My pleasure. Thank you, Pauline!

Collapse
 
theodesp profile image
Theofanis Despoudis

Awesome!

Collapse
 
abdusamikovna profile image
Durdona

Great cheat sheet really handy !

Collapse
 
yaron profile image
Yaron Levi

Thank you for this guide!

Collapse
 
raymag profile image
Carlos Magno

Nice article! It surely helped me a lot, thank you!

Collapse
 
garychan8523 profile image
garychan • Edited

{todos.map(todo => <li key={todo.id}>{todo.text}</li>)}

put the missing bracket for it to work :-)

Collapse
 
reedbarger profile image
Reed Barger

Fixed

Collapse
 
fad16papa profile image
Francis

Hi Reed

Great Article thank you so much you saved my life LOL!.

Collapse
 
klemenm profile image
Klemen

Hi, great article! It is great for me as i am currently learning React! Thank you for your effort!

Collapse
 
aglowiditsolutions profile image
Aglowid It Solutions

Nice Post! Also have a look at Pros and Cons of ReactJS Web Application Development: aglowiditsolutions.com/blog/pros-a...

Collapse
 
west0ne profile image
Oleg Chepil

thx a lot for interesting article!

Collapse
 
suniljoshi19 profile image
Sunil Joshi

Amazing Work, thanks!

Collapse
 
moatazabdalmageed profile image
Moataz Mohammady

Thanks but I cannot downlead the PDF

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
ibrahimcesar profile image
Ibrahim Cesar

Give this guy two trophies everybody, in case he misses one!

Collapse
 
binmansoor11 profile image
Talha Mansoor

Well summarized!

Collapse
 
giandodev profile image
GiandoDev

Hey when is coming the next course one month is pass 😀?