Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
How to use react error boundary in a react typscript application

I was trying to use the react error boundary in my typescript react app but it doesn't seem to be working. Whenever there is an error that breaks the application, the fallback UI does not get rendered

ErrorBoundary.tsx

import React, {Component} from 'react'
class ErrorBoundary extends Component<{children:JSX.Element}, { hasError: boolean }> {
     constructor(props: { children: JSX.Element; } | Readonly<{ children: JSX.Element; }>) {
       super(props)
     
       this.state = {
          hasError: false
       };
       
     }
     static getDerivedStateFromError(error: any) {
          return { hasError: true };
     }
    
  render() {
     if(this.state.hasError == true){
          return <h1>Something went wrong</h1>
     }else{
         return this.props.children;
     }
  }
}

export default ErrorBoundary;

App.tsx

import { useContext, useState, useEffect, FC} from 'react';
import { AccessContexts } from './components/Contexts';
import Context from './components/Contexts';
import { Route, Routes, BrowserRouter} from 'react-router-dom';
import ErrorBoundary from './components/ErrorBoundary';
import Dashboard from './pages/Dashboard';
function App(){
  return (
    <AccessContexts>
        <MyRoutes />
    </AccessContexts>
  );
}

export default App;
function MyRoutes() {
  
     return (
      <div className="App">
        <ErrorBoundary>
          <BrowserRouter>
            <Routes>
              <Route path='/dashboard' element={<Dashboard />}/>
            </Routes>
          </BrowserRouter>
        </ErrorBoundary>
      </div>
     )
}




I am using the ErrorBoundary component in MyRoutes

Thanks for your help

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda