render()
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) { // Update state so the next render will show the fallback UI. return { hasError: true }; }
render() {
if (this.state.hasError) { // You can render any custom fallback UI return <h1>Something went wrong.</h1>; }
return this.props.children;
}
}
static getDerivedStateFromProps(props, state)
componentDidUpdate(prevProps, prevState, snapshot)
getSnapshotBeforeUpdate(prevProps, prevState)
constructor(props)
componentDidCatch(error, info)
UNSAFE_componentWillReceiveProps(nextProps)
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}
componentDidCatch(error, info) { // Example "componentStack": // in ComponentThatThrows (created by App) // in ErrorBoundary (created by App) // in div (created by App) // in App logComponentStackToMyService(info.componentStack); }
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}
class ScrollingList extends React.Component {
constructor(props) {
super(props);
this.listRef = React.createRef();
}
getSnapshotBeforeUpdate(prevProps, prevState) {
// Are we adding new items to the list?
// Capture the scroll position so we can adjust scroll later.
if (prevProps.list.length < this.props.list.length) {
const list = this.listRef.current;
return list.scrollHeight - list.scrollTop;
}
return null;
}
componentDidUpdate(prevProps, prevState, snapshot) {
// If we have a snapshot value, we've just added new items.
// Adjust scroll so these new items don't push the old ones out of view.
// (snapshot here is the value returned from getSnapshotBeforeUpdate)
if (snapshot !== null) {
const list = this.listRef.current;
list.scrollTop = list.scrollHeight - snapshot;
}
}
render() {
return (
<div ref={this.listRef}>{/* ...contents... */}</div>
);
}
}
Recommend
Render Props Caveats Be careful when using Render Props with React.PureComponent
React Render Props Using Props Other Than render
React Render Props Use Render Props for Cross-Cutting Concerns
React Hooks FAQ Performance Optimizations How to read an often-changing value from useCallback?
React Hooks FAQ Performance Optimizations How to avoid passing callbacks down?
React Hooks FAQ Performance Optimizations Are Hooks slow because of creating functions in render?
React Hooks FAQ Performance Optimizations How to create expensive objects lazily?
React Hooks FAQ Performance Optimizations How to memoize calculations?
React Hooks FAQ Performance Optimizations How do I implement shouldComponentUpdate?
React Hooks FAQ Performance Optimizations What can I do if my effect dependencies change too often?
React Hooks FAQ From Classes to Hooks How can I measure a DOM node?
React Hooks FAQ From Classes to Hooks Is there something like forceUpdate?
React Hooks FAQ From Classes to Hooks How do I implement getDerivedStateFromProps?
React Hooks FAQ From Classes to Hooks Why am I seeing stale props or state inside my function?
React Hooks FAQ From Classes to Hooks How to get the previous props or state?
React Hooks FAQ From Classes to Hooks Should I use one or many state variables?
React Hooks FAQ From Classes to Hooks Is there something like instance variables?
React Hooks FAQ Adoption Strategy How to test components that use Hooks?
React Forms Controlled Input Null Value
React Forms Handling Multiple Inputs
React Forms The file input Tag
React Forms Controlled Components
Thinking in React Start With A Mock
React Typechecking With PropTypes Function Components
React Typechecking With PropTypes Default Prop Values
React Typechecking With PropTypes Requiring Single Child
React Typechecking With PropTypes PropTypes
React Typechecking With PropTypes
React Lifting State Up Lifting State Up
React Lifting State Up Writing Conversion Functions
React Lifting State Up Adding a Second Input
ReactDOM Reference createPortal()
ReactDOM Reference findDOMNode()
ReactDOM Reference unmountComponentAtNode()
React Release Channels Next Channel Using the Next Channel for Integration Testing
React Uncontrolled Components The file input Tag
React Uncontrolled Components Default Values