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

189
Visualizações
TypeError: undefined is not an object (evaluating 'event.preventDefault') in ReactJS

Hope you're all good.

I'm having an issue I can't find a concise answer on so I'm going to ask for help here.

I have a function which I'm trying to call in my React Component:

  // Open up calendar day if today is equal to day in API.
  const [openCalendarBox, setOpenCalendarBox] = useState('');

  // Call on post method via axios
  const openCalendarChocolateBox = async (event) => {
    event.preventDefault();

    if (date) {
      // Url where to post
      await axios.post(`http://localhost:5001/open/chocolate`, {
        day: date,
      });

      alert('New day is available to eat!');
    }

    setOpenCalendarBox('');
  };

  openCalendarChocolateBox();

But when I call the function it doesn't work like it's suppose to, I'm instead getting this error in the console:

Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating 'event.preventDefault')

How do I solve this issue? I tried removing the event parameter in my argument but I still want to prevent the page from reloading more than once or at all when calling on my function.

Thanks for any help in advance, take care guys.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you want to load this when the component loads, you don't need an event listener. Simply move the code to a useEffect() hook with an empty dependency array (will only run when component loads).

useEffect(() => {
   if (date) {
      // Url where to post
      await axios.post(`http://localhost:5001/open/chocolate`, {
        day: date,
      });

      alert('New day is available to eat!');
    }

    setOpenCalendarBox('');
}, [])

New problem though, where is date coming from?

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