Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

250
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda