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

315
Visualizações
TypeError: Cannot read properties of undefined (reading 'preventDefault')

How can I use e.preventDefault() with multiple Parameters? I'm getting the error:

TypeError: Cannot read properties of undefined (reading 'preventDefault')

With this code:

import React, { useState, useEffect } from "react";
import './App.css';
import axios from './axios.js';

const removeCustomer = (id) => {
  axios.get(`/customers/delete/${id}`)
  window.location = "/"
}

function addCustomer(name, address, e){
  e.preventDefault();
  console.log("hello");
}

function App() {
  const [customers, setCustomers] = useState([])
  const [name, setName] = useState("")
  const [address, setAddress] = useState("")

  useEffect(() => {
    async function fetchData() {
      const req = await axios.get("/customers");
      setCustomers(req.data);
    }
    fetchData();
  }, [])

  return (
    <>
    <h1 className="title">All Customers</h1>
    <div className="customers">
      {customers.map((customer) => (
        <div onClick={() => {
          removeCustomer(customer.id)
        }} key={customer.id} className="customer">
          <h3>{customer.name}</h3>
          <p>{customer.address}</p>
        </div>
      ))}
    </div>
    <form>
      <input type="text" placeholder="Name" onChange={(e) => setName(e.target.value)}/>
      <input type="text" placeholder="Address" onChange={(e) => setAddress(e.target.value)}/>
      <button type="submit" onClick={() => addCustomer(name, address)}>Add</button>
    </form>
    </>
  );
}

export default App;

I have one question (don't worry if you don't answer it) how can I send the two variables name and address to my backend?

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

0

You are expecting the event to come from where you are calling the function. But you are calling the function with an arrow function. You need to pass the event to function when you perform a click.

// Your js Code will be like this
function addCustomer(name, address, e){
  e.preventDefault();
  console.log("hello");
}

// Your HTML code will be like this
<button type="submit" onClick={(e) => addCustomer(name, address, e)}>Add</button>

This should definitely work.

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