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

128
Visualizações
My functions don't follow the synchronous using React

I don't know why the handleSum function doesn't sum the updated value came from handleInput when I invoke the function handleAll through the onChange input event. It seems that sums with the previous value. Can you bring some light here? Thank you!

Here the JS part:

const card = () => {

//hook defined
const [input, setInput] = React.useState({
    p1h1: "",
    p1h2: "",
});
const [result, setResult] = React.useState("")

//handle input change  
const handleInput = function(e){
    setInput({
        ...input, 
        [e.target.name]: e.target.value
    });
};

//handle sum
const handleSum = function(){
    const { p1h1, p1h2 } = input;            
    setResult(Number(p1h1) + Number(p1h2));
}

   //handle result player
   const handleAll = function (e) {
        handleInput(e)
        setTimeout (()=> {handleSum()},1000) // I inserted this setTimeout to double sure that this is executing after the previous function but didn't work.
    }
}

Here the HTML part:

<input 
  name= "p1h1" 
  value={input.p1h1} 
  onChange={handleAll} 
  type= "number" 
  min="0" 
</input>

<input 
  name= "p1h2" 
  value={input.p1h2} 
  onChange={handleAll} 
  type= "number" 
  min="0" 
</input>

<h2>Result sum: {result}</h2>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

According to the Rules of Hooks (see my comment) you may not use hooks except at the toplevel of functional components. Not nested in other functions, not in if not in for, etc.

So you need to make a functional component, like const Test = () => { return (<div />); }

Then you need to call your hook, and render your HTML as JSX from within that component. You can then use your handleInput function to update the state.

You do not need another state to hold the result of the addition. It's OK to just compute that during render.

This will work:

const Test = () => {
  //hook defined
  const [input, setInput] = React.useState({
    p1h1: '',
    p1h2: '',
  });

  //handle input change
  const handleInput = function (e) {
    setInput({
      ...input,
      [e.target.name]: e.target.value,
    });
  };
  const { p1h1, p1h2 } = input;
  const result = Number(p1h1) + Number(p1h2);

  return (
    <div>
      <input name="p1h1" value={input.p1h1} onChange={handleInput} type="number" min="0"></input>

      <input name="p1h2" value={input.p1h2} onChange={handleInput} type="number" min="0"></input>

      <h2>Result sum: {result}</h2>
    </div>
  );
};
about 4 years ago · Juan Pablo Isaza Relatório

0

The way you are calling handleSum is not right. You should use a useEffect hook to re-render the element when a particular state changes as an array of dependencies (2nd argument of useEffect). You can learn more about it here https://reactjs.org/docs/hooks-effect.html.

const App = () => { 

const [input, setInput] = React.useState({
    p1h1: "",
    p1h2: "",
});
React.useEffect(() => {
    handleSum()
  }, [input])

const [result, setResult] = React.useState("")

//handle input change  
const handleInput = function(e){
    setInput({
        ...input, 
        [e.target.name]: e.target.value
    });
};

//handle sum
const handleSum = function(){
    const { p1h1, p1h2 } = input;            
    setResult(Number(p1h1) + Number(p1h2));
}
  return (
    <div>

      <input
        name= "p1h1" 
        value={input.p1h1} 
        onChange={handleInput} 
        type= "number" 
        min="0">
      </input>

      <input 
        name= "p1h2" 
        value={input.p1h2} 
        onChange={handleInput} 
        type= "number" 
        min="0">
      </input>
    <h2>Result sum: {result}</h2>
</div>

) }

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