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

126
Visualizações
React.js event with arrow function vs without any () function

hello i am wondering why only normal functions like below works and arrow functions do not.

const Form = () =>{

    const [text, setText] = useState("");

    const handle=(e)=>{
       setText(e.target.value);
    }
    const alertText = (e) =>{
       console.log(text);
       alert(text);
    }

    return(
       <>
           <input onChange={handle}></input>
           <button onClick={alertText}>submit</button>
           <h1>{text}</h1>   
       </>
    )}

using arrow functions like <input onChange={()=>handle}> doesn't work at all and I have no idea what the difference between of them.

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

0

Well, that's because if you use an arrow function on the element you have to call it like this:

<input onChange={(e)=>handle(e)}>
about 4 years ago · Juan Pablo Isaza Relatório

0

component expects onChange as a function, that function will be called when the user changes the value of the input.

NOTE: It's syntactically correct but the handle function will not execute.

When you do

<input onChange={handle}></input>
// it's equivalent to
<input onChange={(e)=>{ setText(e.target.value); }}></input>

But when you do

<input onChange={()=>handle}></input>
// it's equivalent to
<input onChange={(e) => ()=>{ setText(e.target.value);}}></input>
about 4 years ago · Juan Pablo Isaza Relatório

0

You can do:

 <input onChange={(e)=>handle(e)}>

You are not passing the event into the handle function

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