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

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

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 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!