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 |
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 |
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.)
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:
// 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, | |
}; |
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:
// 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, |
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: |
const COLORS = { | |
background: { | |
bgBlack: { | |
browser: [0, 0, 0], | |
node: "\x1b[40m", | |
}, | |
bgRed: { | |
browser: [255, 0, 0], | |
node: "\x1b[41m", | |
}, |
/** 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() |