Error Boundary is not directing users to new path

59 views Asked by At

I am using an ErrorBoundary to catch errors and display an error message if something cannot be found.

The error boundary:

'use client';
import React, { Component, ErrorInfo, ReactNode } from 'react';

interface Props {
  children?: ReactNode;
}

interface State {
  hasError: boolean;
}

class ErrorBoundary extends Component<Props, State> {
  public state: State = {
    hasError: false,
  };

  public static getDerivedStateFromError(_: Error): State {
    // Update state so the next render will show the fallback UI.
    return { hasError: true };
  }

  public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
    console.error("Uncaught error:", error, errorInfo);
  }

  public render() {
    if (this.state.hasError) {
      return (
        <div>
          <h1>Oops! Looks like something went wrong.</h1>
          <p>You might want to double-check your link and try again. (404)</p>
        </div>
      );
    }

    return this.props.children;
  }
}

export default ErrorBoundary;

It catches the error, but when the user clicks on a navigation button it does not redirect the user to that page.

It changes the url/pathname and if the user reloads, they'll go to the right page.

I've tried adding a useEffect to the nav bar to push when the pathname changes.

0

There are 0 answers