| 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 |
| // This is an example of how to fetch external data in response to updated props, | |
| // If you are using an async mechanism that does not support cancellation (e.g. a Promise). | |
| class ExampleComponent extends React.Component { | |
| _currentId = null; | |
| state = { | |
| externalData: null | |
| }; |
React recently introduced an experimental profiler API. This page gives instructions on how to use this API in a production release of your app.
Table of Contents
React DOM automatically supports profiling in development mode for v16.5+, but since profiling adds some small additional overhead it is opt-in for production mode. This gist explains how to opt-in.
React recently introduced an experimental profiler API. After discussing this API with several teams at Facebook, one common piece of feedback was that the performance information would be more useful if it could be associated with the events that caused the application to render (e.g. button click, XHR response). Tracing these events (or "interactions") would enable more powerful tooling to be built around the timing information, capable of answering questions like "What caused this really slow commit?" or "How long does it typically take for this interaction to update the DOM?".
With version 16.4.3, React added experimental support for this tracing by way of a new NPM package, scheduler. However the public API for this package is not yet finalized and will likely change with upcoming minor releases, so it should be used with caution.
Copyright © <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
| /** 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() |
| // 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, | |
| }; |
| 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 |
| #!/usr/bin/env node | |
| const { spawn } = require("child_process"); | |
| const util = require("node:util"); | |
| const exec = util.promisify(require("node:child_process").exec); | |
| async function main() { | |
| let { REPLAY_CHROME, RECORD_REPLAY_API_KEY } = process.env; | |
| if (!REPLAY_CHROME || !RECORD_REPLAY_API_KEY) { |
