Error recovery with reset helps your app fix problems by letting users try again without reloading the whole page.
Error recovery with reset in NextJS
import React from 'react'; class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false, resetKey: 0 }; } static getDerivedStateFromError(error) { return { hasError: true }; } resetError = () => { this.setState((state) => ({ hasError: false, resetKey: state.resetKey + 1 })); }; render() { if (this.state.hasError) { return ( <div> <p>Something went wrong.</p> <button onClick={this.resetError}>Try again</button> </div> ); } return ( <div key={this.state.resetKey}> {this.props.children} </div> ); } }
This example uses a React class component's state to track errors.
The resetError method clears the error state so the component can try again.
function ErrorFallback({ reset }) { return ( <div role="alert"> <p>Oops! An error happened.</p> <button onClick={reset}>Reset</button> </div> ); }
import React from 'react'; class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { error: null, resetKey: 0 }; } static getDerivedStateFromError(error) { return { error }; } resetError = () => { this.setState((state) => ({ error: null, resetKey: state.resetKey + 1 })); }; render() { if (this.state.error) { return <ErrorFallback reset={this.resetError} />; } return ( <div key={this.state.resetKey}> {this.props.children} </div> ); } }
This app has a button that counts up. When it reaches 5, it crashes. The ErrorBoundary catches the crash and shows a message with a reset button. Clicking reset lets you try counting again from zero.
import React, { useState } from 'react'; function BuggyCounter() { const [count, setCount] = useState(0); if (count === 5) { throw new Error('I crashed!'); } return ( <button onClick={() => setCount(count + 1)}> Count: {count} </button> ); } class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false, resetKey: 0 }; } static getDerivedStateFromError(error) { return { hasError: true }; } resetError = () => { this.setState((state) => ({ hasError: false, resetKey: state.resetKey + 1 })); }; render() { if (this.state.hasError) { return ( <div> <p>Something went wrong.</p> <button onClick={this.resetError}>Try again</button> </div> ); } return ( <div key={this.state.resetKey}> {this.props.children} </div> ); } } export default function App() { return ( <main> <h1>Error Recovery with Reset</h1> <ErrorBoundary> <BuggyCounter /> </ErrorBoundary> </main> ); }
React error boundaries catch errors during rendering but not in event handlers.
Resetting error state lets users recover without reloading the page.
Next.js supports React error boundaries like this for better user experience.
Error recovery with reset helps keep your app running smoothly after a problem.
Use a reset button to let users try again without full reload.
React class components help build simple error boundaries.