Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!