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

255
Visualizações
I can't pass parameters to external function to render the message, TypeError: Class constructor AlertMessage cannot be invoked without 'new'

whenever I try to send a message as a parameter to the showAlert() function it says:

TypeError: Class constructor AlertMessage cannot be invoked without 'new' 

image: https://imgur.com/X6PIMHf

I just want to pass the message as a parameter and render it back to the user. I tried to pass props directly to the AlertMessage class but it didn't work as it return an empty alert. if my question is still unclear ask me to provide more data.

   import showAlert from "../components/toast";

         ...

    export async function getStaticProps(){
      //fetch data from an API
      const res = await fetch("http://localhost:3000/api/posts");
      const foundData = await res.json();
          
      if(!foundData){
        return{
          notFound: true,
        }
      }
    
      if(foundData.status == 500){
         showAlert("error");
    
        return{
          notFound: true,
        }
      }else{
        return {
          props: {
            data: foundData.foundPosts
          },
          revalidate: 1 //time per seconds
        }
      }
    
    }

//toats .js code

import { Component } from "react";

export function showAlert(message){
    const myAlert = document.getElementById("myAlert");
    const alertMessage = document.getElementById("alertMessage");
    const alert = new bootstrap.Toast(myAlert);
    alertMessage.textContent = message;
    alert.show();
}

export function HideALert(){...}

export default class AlertMessage extends Component{

    render(){   
        return(
            <div className="position-fixed bottom-0 end-0 p-3" style={{zIndex: "11"}}>
                <div className="toast align-items-center text-white bg-danger border-0" id="myAlert" role="alert" aria-live="assertive" aria-atomic="true">
                <div className="d-flex">
                    <div className="toast-body" id="alertMessage">
                        There was an error. Please try again
                    </div>
                    <button type="button" className="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
                </div>
                </div>
            </div>
        );
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I fixed it by passing the error on the props first.

  if(foundData.status == 500){ 
    return {
      props: {
        statusCode: foundData.status,
        error: foundData.status // here I pass the message I want to show
      }
    };
}

then checking if this error prop exists, if it does then I check if the window is defined (to know if DOM exists or not) then I show the message.

 if(props.error){
    if(typeof window !== "undefined"){
      showAlert(props.error);
    };
  }
about 4 years ago · Juan Pablo Isaza Relatório
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