React Deep Force Update
react-deep-force-update is a specialized utility library designed to recursively force-update a React component tree, bypassing standard `shouldComponentUpdate` checks. It is explicitly *not* intended for use in production application code, but rather for React development tools, such as hot module reloading or debugging utilities, where a deep update is necessary regardless of component lifecycle optimizations. The current stable version is 2.1.3. It has a very slow release cadence, typically updating only when core React features or deprecations necessitate changes, such as the shift to React 0.14. Its key differentiator is its ability to bypass standard React update mechanisms for development-time introspection and manipulation, providing a granular control not available through standard React APIs. This package serves a niche purpose within the React ecosystem.
Common errors
-
Uncaught TypeError: Cannot read property 'getPublicInstance' of undefined
cause Attempting to use `deepForceUpdate` with an invalid React instance or a null/undefined value.fixEnsure the argument passed to `deepForceUpdate` is a valid, mounted React component instance, typically obtained from `ReactDOM.render()` or `ReactDOM.createRoot().render()`. -
Warning: Only ES6 classes that inherit React.Component will be properly updated when walking the tree.
cause Using `react-deep-force-update` v2.0.0+ with components that do not extend `React.Component` (e.g., using `createClass` or older ES6 class patterns).fixRefactor your components to be ES6 classes that explicitly `extend React.Component` to ensure they are fully compatible with deep force updates in newer versions of the library.
Warnings
- breaking Version 2.0.0 and later require React 0.14 or newer. Using it with older React versions (e.g., 0.13) will lead to runtime errors or unexpected behavior.
- breaking As of v2.0.0, only ES6 class components that explicitly inherit `React.Component` will be properly updated when traversing the component tree. Components defined via `createClass()` or other deprecated patterns are no longer fully supported for deep updates.
- gotcha This library is strictly for React development tools and should *never* be used in production application code. Its purpose is to override standard React update mechanisms, which can lead to unpredictable behavior and performance issues if used inappropriately in a live application.
- gotcha For React Native applications, versions of `react-deep-force-update` prior to a fix for facebook/react-native#2985 may not work correctly. Users were advised to stick to 1.x for React Native until the issue was resolved.
Install
-
npm install react-deep-force-update -
yarn add react-deep-force-update -
pnpm add react-deep-force-update
Imports
- deepForceUpdate
const deepForceUpdate = require('react-deep-force-update');import deepForceUpdate from 'react-deep-force-update';
Quickstart
import React from 'react';
import { render } from 'react-dom';
import deepForceUpdate from 'react-deep-force-update';
// A simple React component for demonstration
class MyComponent extends React.Component {
render() {
return <div>Hello, {this.props.name}</div>;
}
}
// Render the component into the DOM
const container = document.createElement('div');
document.body.appendChild(container);
const instance = render(<MyComponent name="World" />, container);
// Simulate an update scenario (e.g., from a dev tool)
setTimeout(() => {
console.log('Forcing deep update...');
deepForceUpdate(instance);
console.log('Deep update complete. Check the console for any React warnings if applicable.');
}, 1000);