简体   繁体   中英

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>
        );
    }
}

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);
    };
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM