DEV Community

Cover image for React Cheat sheet (Updated June 2021)

React Cheat sheet (Updated June 2021)

Eric The Coder on June 11, 2021

Follow me!: Follow @EricTheCoder_ I don't use React often and so whenever I need to do even the smallest thing in React, I have to check out the...
Collapse
 
xardonik profile image
XardoniK • Edited

I found small mistake in "e for event arguments":

This won't work because you pass string as an argument

const clickHandler = (e) => console.log(e.target)
function App(){
    return (
        <>
            <h1>Welcome to my app</h1>
            <button onClick={() => clickHandler(*'Hello World'*)}>Say Hi</button>
        </> 
    )
} 
Enter fullscreen mode Exit fullscreen mode

There are two ways how to do it:
1.

const clickHandler = (e) => console.log(e.target)
function App(){
    return (
        <>
            <h1>Welcome to my app</h1>
            <button onClick={(e) => clickHandler(e)}>Say Hi</button>
        </> 
    )
} 
Enter fullscreen mode Exit fullscreen mode

2.

const clickHandler = (e) => console.log(e.target)
function App(){
    return (
        <>
            <h1>Welcome to my app</h1>
            <button onClick={clickHandler}>Say Hi</button>
        </> 
    )
} 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ericchapman profile image
Eric The Coder

Ok thanks I made the correction

Collapse
 
elibates profile image
Eli Bates

Thanks! Will use

Collapse
 
linusvallejo profile image
LinusVallejo

Exactly

Collapse
 
ccrystle profile image
Charlie

thanks--helpful post.

there's a typo: import {Greeting} from './Gretting'

Collapse
 
guptaarth87 profile image
guptaarth87

This post was amazing actually it is hard to find all essenstial things of any particular technology at one place. You almost covered all topics that a beginner should know but I think u should have also added the routing topic.

Collapse
 
joshternyak profile image
Josh Ternyak

Super in-depth post! I just launched a site bitcoinforecast.io and I developed it myself using React. Your post covers the strategies I used to built the app. "Props" off to you ;) pun intended.

Collapse
 
zorig profile image
ZG

I think, the word deconstructing needed to be changed as destructuring

Collapse
 
ericchapman profile image
Eric The Coder

Yeah good catch. Thanks.

Collapse
 
charlotteisambert profile image
Charlotte Isambert • Edited

Thanks a lot that's very helpful!

There is one thing I don't understand in this part from 'setState functional form' though:

function Counter() {
  const [count, setCount] = useState(0)
  // Use a function to set State
  const increase = () => setCount(() => count + 1)
  return (
    <>
      <h1>Counter</h1>
      <p>{count}</p>
      <button onClick={increase} className='btn'> + </button>
      <button onClick={() => setCount(() => count - 1)} className='btn'> - </button>
    </>
  )
}
Enter fullscreen mode Exit fullscreen mode

I don't understand why we'd use the functional update of setState without its count param: setCount(() => count - 1).

I would understand if we used the count param from setCount: setCount((count) => count - 1), to make sure we use the last count value

Am I missing something? :)

Collapse
 
axmachina profile image
AxMachina

Your version is accurate. The point of using functional form is to ensure you get the most current state value and not the stale one (as in the original version).

Collapse
 
hunterachieng profile image
hunterachieng

Thank you for this post

Collapse
 
amboulouma profile image
Amin M. Boulouma
Collapse
 
safinghoghabori profile image
Safin Ghoghabori

Can you provide next parts please? If possible add link to this so we can access it easily.

Collapse
 
dova profile image
doVa

Can you add Reducer and Context?

Collapse
 
boly38 profile image
Brice

There is multiples examples without only one root element in a return.
Anyway thanks for this good sharing 👍

Collapse
 
pekosong profile image
Peko • Edited

Thank you posting

this component will not work.

const Person = (name, age) => {
return (

Name: {name}, Age: {age}

)
}

should be

const Person = ({name, age}) => {

return (

Name: {name}, Age: {age}


)

}
Collapse
 
ericchapman profile image
Eric The Coder

Fixed. Thanks

Collapse
 
yashlalpotu11 profile image
YASH LALPOTU

Very helpful, thanks

Collapse
 
dylut2000 profile image
Hardy Lutula

Thank you so much..

Collapse
 
atuljaiswar profile image
atuljaiswar

First of all this is not 2021 cheat sheet FYI I am using all this from the end of 2019.
Please be sure before ur posting anything it's not 2021 cheat sheet

Collapse
 
cags84 profile image
Carlos Guzman

Thanks

Collapse
 
adithya_baliga profile image
Adithya Baliga

Wow! very useful and informative post

Collapse
 
kunal__satpute profile image
Kunal Satpute

Thank you for this post Eric

Collapse
 
davidcam profile image
David Camarena

Really nice and useful, thanks!

Collapse
 
gracestefan12 profile image
Grace Stefan

Great post.

Collapse
 
andrewbaisden profile image
Andrew Baisden

This is a great detailed list.

Collapse
 
melfordd profile image
Melford Birakor

Well done... It's not an easy task to make such compilations across topics