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

322
Views
React question: Why do I have to use the anonymous functions in onClick

I am learning React and doing the Todo list practice these days, here is my code:

import React, {useState} from 'react'

export default function App() {
    const[test, setTest] = useState('')
    const[list, setList] = useState(['aa','bb','cc'])   
    const handleChange = (evt) =>{
        setTest(evt.target.value)
    }

    const handleAdd = () =>{
        setList([...list, test])       
        setTest('')
    }

    const handleDelete = (index) =>{
        var newlist = [...list]       
        newlist.splice(index,1)
        setList(newlist)
    }
  return (
    <div>
        <input onChange={handleChange} value={test}/>
        <button onClick = {handleAdd}>add</button>
        <ul>
            {list.map((item,index)=>
                <li key={index}> 
                {item} 
                <button onClick={()=>{
                    handleDelete(index)
                }}>delete</button> 
                </li>)}
        </ul>
    </div>
  )
}

I know it is a stupid question but I was so confused that why I can not write the last onClick function like this:

<button onClick={handleDelete(index)}>delete</button> 

what is the role of the anonymous function?

Thanks for your patience.

about 4 years ago · Juan Pablo Isaza
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!