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

106
Views
Getting Synthetic Base Event in console instead of that particular item

Below is the code. Whenever I am clicking on add to cart, for every item. I can see an alert. And Instead of item if I console.log something static it is also working but with item. I am getting a Synthetic Base Event in console

function Product() {
    useEffect (() => {
        fetchItems();
    },[]);

    const [items, setItems] = useState([]);

    const fetchItems = async() => {
        const data = await fetch('/product',{
            method: "GET",
            headers: {
                Accept: "application/json",
                "Content-Type": "application/json"
            },
            credentials: "include"
        });
        const items = await data.json();
        setItems(items);
    };

    const clickMe = (item) => {
        alert("Working");
        console.log(item);
    };

    return (
        <div>
            <h1>Product Page</h1>

            {
                items.map(item => (
                    <p>{item.productname} {item.productprice}  {item.type} <a onClick={clickMe.bind(item)}>add to cart</a> </p>
                ))
            }

        </div>
    )
}

export default Product
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here .bind doesn't do what you think it does.

Function.prototype.bind

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

With onClick={clickMe.bind(item)} you are only binding item to the this of the function and the onClick event is still passed as the first argument to the callback.

If you are wanting to pass the currently mapped item then this needs to be passed as an additional argument.

onClick={clickMe.bind(null, item)}

You could also just use an anonymous callback function.

onClick={() => clickMe(item)}

Or declare clickMe to be a curried function and return the click handler with the item closed over in callback scope.

const clickMe = (item) => () => {
    alert("Working");
    console.log(item);
};

...

onClick={clickMe(item)}
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!