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

244
Views
No puedo pasar parámetros a una función externa para representar el mensaje, TypeError: el constructor de clases AlertMessage no se puede invocar sin 'nuevo'

cada vez que intento enviar un mensaje como parámetro a la función showAlert() dice:

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

imagen: https://imgur.com/X6PIMHf

Solo quiero pasar el mensaje como un parámetro y devolverlo al usuario. Traté de pasar accesorios directamente a la clase AlertMessage pero no funcionó porque devolvió una alerta vacía. Si mi pregunta aún no está clara, pídeme que proporcione más datos.

 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 código .js

 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

Lo arreglé pasando el error en los accesorios primero.

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

luego verifico si existe este accesorio de error, si existe, verifico si la ventana está definida (para saber si DOM existe o no) y luego muestro el mensaje.

 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!