Skip to content

Instantly share code, notes, and snippets.

@bvaughn
bvaughn / react-lifecycle-cheatsheet.md
Last active March 2, 2023 13:29
React lifecycle cheatsheet

React lifecycle cheatsheet

Method Side effects1 State updates2 Example uses
Mounting
componentWillMount Constructor equivalent for createClass
render Create and return element(s)
componentDidMount DOM manipulations, network requests, etc.
Updating
componentWillReceiveProps Update state based on changed props
@bvaughn
bvaughn / react-dom-test-selectors.md
Last active January 9, 2023 15:46
Experimental React DOM test selector API

RFC Test Selectors

Owner: Brian Vaughn


As of facebook/react/pull/22760, the experimental Test Selector API is now available in the experimental release channel.

To test the API, first install the experimental release:

const createLogger = (backgroundColor, color) => {
const logger = (message, ...args) => {
if (logger.enabled === false) {
return;
}
console.groupCollapsed(
`%c${message}`,
`background-color: ${backgroundColor}; color: ${color}; padding: 2px 4px;`,
...args
@bvaughn
bvaughn / React.unstable_Profiler.md
Last active December 17, 2022 00:48
Notes about the in-development React <Profiler> component

Profiler

React 16.4 will introduce a new Profiler component (initially exported as React.unstable_Profiler) for collecting render timing information in order to measure the "cost" of rendering for both sync and async modes.

Profiler timing metrics are significantly faster than those built around the User Timing API, and as such we plan to provide a production+profiling bundle in the future. (The initial release will only log timing information in DEV mode, although the component will still render its children- without timings- in production mode.)

How is it used?

Profiler can be declared anywhere within a React tree to measure the cost of rendering that portion of the tree. For example, a Navigation component and its descendants:

@bvaughn
bvaughn / eager-prefetching-async-data-example.js
Last active November 30, 2022 21:16
Advanced example for eagerly prefetching async data in a React component.
// This is an advanced example! It is not intended for use in application code.
// Libraries like Relay may make use of this technique to save some time on low-end mobile devices.
// Most components should just initiate async requests in componentDidMount.
class ExampleComponent extends React.Component {
_hasUnmounted = false;
state = {
externalData: null,
};
@bvaughn
bvaughn / devtools-bridge-protocol.md
Last active October 13, 2022 18:34
React DevTools: Unsupported backend version

Unsupported DevTools backend version

This page contains instructions for updating a local React DevTools application to match a version embedded in a renderer such as React Native. Instructions below cover NPM, Flipper, and React Native Debugger. If you use React DevTools in a different way, please let us know.

If you are viewing this page, you have likely seen one of the dialogs below:

Dialog displaying downgrade instructions for the React DevTools frontend to connect to an older backend version

@bvaughn
bvaughn / attaching-manual-event-listeners-in-passive-effect.js
Last active July 6, 2022 23:34
Attaching manual event listeners in a passive effect
// Simplistic (probably most common) approach.
//
// This approach assumes either that:
// 1) passive effects are always run asynchronously, after paint, or
// 2) passive effects never attach handlers for bubbling events
//
// If both of the above are wrong (as can be the case) then problems might occur!
useEffect(() => {
const handleDocumentClick = (event: MouseEvent) => {
// It's possible that a "click" event rendered the component with this effect,
@bvaughn
bvaughn / setupFilesAfterEnv.js
Created August 23, 2021 21:37
Jest config to remove extra console location / formatting noise
import { CustomConsole } from '@jest/console';
function formatter(type, message) {
switch(type) {
case 'error':
return "\x1b[31m" + message + "\x1b[0m";
case 'warn':
return "\x1b[33m" + message + "\x1b[0m";
case 'log':
default:
@bvaughn
bvaughn / console-colors.js
Last active July 6, 2022 23:32
Color logging utils for Node and the browser
const COLORS = {
background: {
bgBlack: {
browser: [0, 0, 0],
node: "\x1b[40m",
},
bgRed: {
browser: [255, 0, 0],
node: "\x1b[41m",
},
@bvaughn
bvaughn / react-virtualized-framerate-test.js
Last active May 27, 2022 18:57
Quick demonstration of a way to measure scrolling performance for react-virtualized in an automated way
/** Measures framerate for the time between start() and stop() calls */
function FramerateMeasurer () {
this.start = () => {
this._beginTime = ( performance || Date ).now()
this._frames = 0
this._animationFrameId = requestAnimationFrame(this._loop)
}
this.stop = () => {
const endTime = ( performance || Date ).now()