React Lifecycle Cheat Sheet



  • Lifecycle Hooks const ruc tor Called before component is mounted render Never call setState here. React Reusability Cheat Sheet by hackingbeauty.
  • A cheat sheet that contains common enumeration and attack methods for Windows Active Directory. React Redux Cheatsheet ⭐ 1,423 React Redux Cheat Sheet on Workflow & Concept.
  • React Cheat Sheet.

React: useEffect explained with lifecycle methods Sometimes, we'd like only a single of those lifecycle methods to run. The following cheat-sheet will help you achieve the 'effect' you're looking for. React's useEffect hook combines componentDidMount, componentDidUpdate and componentWillUnmount lifecycle methods. A javascript libra ry for building user interfaces. React Cheat Sheet DEMO: GITHUB: https://github.com/facebook/react.

A short guide to all the exported functions in React Testing Library

  • renderconst {/* */} = render(Component) returns:
    • unmount function to unmount the component
    • container reference to the DOM node where the component is mounted
    • all the queries from DOM Testing Library, bound to the document so thereis no need to pass a node as the first argument (usually, you can use thescreen import instead)
import{ render, fireEvent, screen }from'@testing-library/react'
React lifecycle cheat sheet
test('loads items eventually',async()=>{
React
fireEvent.click(getByText('Load'))
// Wait for page to update with query text
const items =await screen.findAllByText(/Item #[0-9]: /)

React Lifecycle Cheat Sheet Printable

})

Queries#

Difference from DOM Testing Library

The queries returned from render in React Testing Library are the same asDOM Testing Library except they have the first argument bound to thedocument, so instead of getByText(node, 'text') you do getByText('text')

See Which query should I use?

No Match1 Match1+ MatchAwait?
getBythrowreturnthrowNo
findBythrowreturnthrowYes
queryBynullreturnthrowNo
getAllBythrowarrayarrayNo
findAllBythrowarrayarrayYes
queryAllBy[]arrayarrayNo
  • ByLabelText find by label or aria-label text content
    • getByLabelText
    • queryByLabelText
    • getAllByLabelText
    • queryAllByLabelText
    • findByLabelText
    • findAllByLabelText
  • ByPlaceholderText find by input placeholder value
    • getByPlaceholderText
    • queryByPlaceholderText
    • getAllByPlaceholderText
    • queryAllByPlaceholderText
    • findByPlaceholderText
    • findAllByPlaceholderText
  • ByText find by element text content
    • getByText
    • queryByText
    • getAllByText
    • queryAllByText
    • findByText
    • findAllByText
  • ByDisplayValue find by form element current value
    • getByDisplayValue
    • queryByDisplayValue
    • getAllByDisplayValue
    • queryAllByDisplayValue
    • findByDisplayValue
    • findAllByDisplayValue
  • ByAltText find by img alt attribute
    • getByAltText
    • queryByAltText
    • getAllByAltText
    • queryAllByAltText
    • findByAltText
    • findAllByAltText
  • ByTitle find by title attribute or svg title tag
    • getByTitle
    • queryByTitle
    • getAllByTitle
    • queryAllByTitle
    • findByTitle
    • findAllByTitle
  • ByRole find by aria role
    • getByRole
    • queryByRole
    • getAllByRole
    • queryAllByRole
    • findByRole
    • findAllByRole
  • ByTestId find by)
  • configure change global options:configure({testIdAttribute: 'my-data-test-id'})
  • cleanup clears the DOM (use with afterEach to resetDOM between tests)

Text Match Options#

Given the following HTML:

Will find the div:

React Lifecycle Cheat Sheet
getByText('Hello World')// full string match
getByText('llo Worl',{ exact:false})// substring match
getByText('hello world',{ exact:false})// ignore case
React

What Is The Order In React Lifecycle

// Matching a regex:
getByText(/world/i)// substring match, ignore case
getByText(/^hello world$/i)// full string match, ignore case

React Component Lifecycle Events

getByText((content, element)=> content.startsWith('Hello'))