DEV Community

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

Posted on • Updated 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 app
npx create-react-app my-app-name

// Run the created app
cd my-app-name
yarn start

// http://localhost:3000
Enter fullscreen mode Exit 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 component
function App(){
  return <h1>Hello World</h1>
} 

export default App;
Enter fullscreen mode Exit 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 mode Exit 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 valid
return <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 mode Exit fullscreen mode

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

// not valid
return (
    <div class="title">
        Hello World
    </div>
)

// valid
return (
    <div className="title">
    </div>
)

Enter fullscreen mode Exit fullscreen mode

Close every element

return (
    <img src="http:example.com/image.jpg" />
    <input type="text" name="first_name" />
)
Enter fullscreen mode Exit fullscreen mode

Nested Components

// Arrow function shorthand component
const Person = () => <h1>Mike Taylor</h1>

// Arrow function component
const Message = () => {
    return <h1>Hello</h1>
}

// Function component
function HelloWorld(){
  return (
      <>
          <Message />
          <Person />
      </>
  )
} 
Enter fullscreen mode Exit fullscreen mode

Component CSS

(src/App.css)

h1 {
    color: red;
}

Enter fullscreen mode Exit fullscreen mode

(src/App.js)
Import the CSS file

import './App.css'

function App(){
  return <h1>Hello World</h1>
} 
Enter fullscreen mode Exit fullscreen mode

Inline CSS

function App(){
  return <h1 style={{ color: 'red' }}>Hello World</h1>
} 
Enter fullscreen mode Exit fullscreen mode

Javascript in JSX

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

Component Properties (Props)

function App()
    return <Person name='Mike' age={29} />
} 

const Person = (props) => {
    return <h1>Name: {props.name}, Age: {props.age}</h1>
}

// or props object deconstructing
const Person = ({name, age}) => {
    return <h1>Name: {name} Age: {age}</h1>
}
Enter fullscreen mode Exit fullscreen mode

Children Props (slot)

function App()
    return (
        <Person name='Mike' age={29}>
            Hi, this is a welcome message
        </Person>
    )
} 

const Person = (props) => {
    return (
        <h1>Name: {props.name}, Age: {props.age}</h1>
        <p>{props.children}</p>
    )
}

// or props object deconstructing
const Person = ({name, age, children}) => {
    return (
        <h1>Name: {name} Age: {age}</h1>
        <p>{children}</p>
    )
}
Enter fullscreen mode Exit 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)

Collapse
 
michaelcurrin profile image
Michael Currin • 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...

Collapse
 
cyrdodi_77 profile image
Dodi Yulian

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

Collapse
 
nashmeyah profile image
Nashmeyah

This is so dang helpful!