Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for React (work in progress) Cheat sheet
Eric The Coder
Eric The Coder

Posted on • Edited on

     

React (work in progress) Cheat sheet

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 documentation, a tutorial, or post a question on a forum.

That's why I decided to do this memory aid and given that my memory is not that good I thought why not make a monstrous memory aid with all the concepts I know about React.

So I can read it from time to time and thereby strengthen my knowledge of React.

It will take couples of days to put all that together so every day I will post an updated Cheat Sheet version until the final version.

If you have ideas or recommendation do not hesitate and do so in the comments section.

Let's start right now with the first version...

React Cheat Sheet (draft day 1)

 

Create a React App

// Create a new appnpxcreate-react-appmy-app-name// Run the created appcdmy-app-nameyarnstart// http://localhost:3000
Enter fullscreen modeExit fullscreen mode

First React functional Component

  • No need to import React from 'react' (since React 17)
  • Must have uppercase first letter
  • Must return JSX

(src/App.js)

// React componentfunctionApp(){return<h1>Hello World</h1>}exportdefaultApp;
Enter fullscreen modeExit fullscreen mode

How this component get render to the browser? The main project file is src/index.js and in that file there are instruction to render the component

ReactDOM.render(<App/>,document.getElementById('root'))
Enter fullscreen modeExit fullscreen mode

The App component will then be rendered inside public/index.html 'root' div

JSX Rules

Return a single element (only one parent element)

// not validreturn<h1>Hello world</h1><h2>Hi!</h2>// valid with fragment.return(<><h1>Hello World</h1><h2>Hi!</h2></>)// Noted the parenthesis for multi-line formatting
Enter fullscreen modeExit fullscreen mode

Use className instead of class
Also all attribute name need to be camelCase

// not validreturn(<divclass="title">        Hello World</div>)// validreturn(<divclassName="title"></div>)
Enter fullscreen modeExit fullscreen mode

Close every element

return(<imgsrc="http:example.com/image.jpg"/><inputtype="text"name="first_name"/>)
Enter fullscreen modeExit fullscreen mode

Nested Components

// Arrow function shorthand componentconstPerson=()=><h1>Mike Taylor</h1>// Arrow function componentconstMessage=()=>{return<h1>Hello</h1>}// Function componentfunctionHelloWorld(){return(<><Message/><Person/></>)}
Enter fullscreen modeExit fullscreen mode

Component CSS

(src/App.css)

h1{color:red;}
Enter fullscreen modeExit fullscreen mode

(src/App.js)
Import the CSS file

import'./App.css'functionApp(){return<h1>Hello World</h1>}
Enter fullscreen modeExit fullscreen mode

Inline CSS

functionApp(){return<h1style={{color:'red'}}>Hello World</h1>}
Enter fullscreen modeExit fullscreen mode

Javascript in JSX

  • Enclose between {}
  • Must be an expression (return a value)
functionApp(){constname='Mike'return(<><h1>Hello{name}</h1><p>{name==='Mike'?'(admin)':'(user)'}</p></>)}
Enter fullscreen modeExit fullscreen mode

Component Properties (Props)

functionApp()return<Personname='Mike'age={29}/>}constPerson=(props)=>{return<h1>Name:{props.name}, Age:{props.age}</h1>}// or props object deconstructingconstPerson=({name,age})=>{return<h1>Name:{name} Age:{age}</h1>}
Enter fullscreen modeExit fullscreen mode

Children Props (slot)

functionApp()return(<Personname='Mike'age={29}>            Hi, this is a welcome message</Person>)}constPerson=(props)=>{return(<h1>Name:{props.name}, Age:{props.age}</h1><p>{props.children}</p>)}// or props object deconstructingconstPerson=({name,age,children})=>{return(<h1>Name:{name} Age:{age}</h1><p>{children}</p>)}
Enter fullscreen modeExit fullscreen mode

Conclusion

That's it for today. We still have a lot to do, so see you tomorrow... If you want to be sure to miss nothing click follow me!

Follow me!:Follow @EricTheCoder_


Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
michaelcurrin profile image
Michael Currin
I'm a self-taught dev focused on websites and Python development.My friends call me the "Data Genie".When I get bored, I find tech to read about, write about and build things with.
  • Location
    Netherlands
  • Education
    Marketing honors + self-taught dev
  • Work
    Senior Software Engineer at ACT Commodities
  • Joined
• Edited on• Edited

Hi. I am not so familiar with React. so this looks useful for me.

I have a React cheatsheet here. I liked some of your points so worked them into my JSX page. Also I hadn't see destructuring props without props variable so added it to my Props page.
michaelcurrin.github.io/dev-cheats...

I also like to use the result of running the quickstart command as repo and live site, for reference.

github.com/MichaelCurrin/react-qui...

CollapseExpand
 
nashmeyah profile image
Nashmeyah
  • Location
    Salt Lake City, UT
  • Work
    Junior Software Engineer
  • Joined

This is so dang helpful!

CollapseExpand
 
cyrdodi_77 profile image
Dodi Yulian
  • Joined

Thank you, it's way better than my cheat sheet, probably I'll use yours

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Businessman and blogger #Javascript, #Python and #PHP. My favorite frameworks/librairies are #React, #Laravel, and #Django. I am also a fan of #TailwindCSS
  • Location
    Canada
  • Joined

More fromEric The Coder

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp