Skip to content

mhmohon/react-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

React Cheatsheets

Table of Contents

Section 1: Setup React

VS Code Extensions

Hooks

Hooks are supported in @types/react from v16.8 up.

useState

Type inference works very well for simple values:

const [count, setCount] = useState(initialCount);
// `count` is inferred to be variable name
// `setCount` a function is used to update the count
// `initialCount` initial value

Example:

import React, { useState } from 'react';

function Example() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

Never modify the state manually.

useEffect / useLayoutEffect

By default, React runs the effects after every render — including the first render

  // Similar to componentDidMount and componentDidUpdate:
  useEffect(() => {
    // Update the document title using the browser API
    document.title = `You clicked ${count} times`;
  }, [count]);
  // `[]` dependency array for the trigger useEffect function. If it is empty then it will load only initalComponent load.
  // `[count]` the trigger variable names. useEffect will call each time those value will changed.

Example:

import React, { useState, useEffect } from 'react';

function Example() {
  const [count, setCount] = useState(0);
  
  useEffect(() => {
    document.title = `You clicked ${count} times`;
  }, [count]);
 
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

Reference

Contributors

This project follows the all-contributors specification. See CONTRIBUTORS.md for the full list. Contributions of any kind welcome!

About

a cheat sheet with react & advanced

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published