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

1.3K
Visualizações
Alert is empty when the value is present in the input [React.js]

I'm trying to alert with the text box value. In the below code, It'll alert whatever the value is in the input.

import { useState } from "react";

export default function App() {
  const [value, setValue] = useState("");

  function handle() {
    alert(value);
  }

  return (
    <div className="App">
      <h1>Hello App</h1>
      <input
        type="text"
        onChange={(e) => {
          setValue(e.target.value);
        }}
      />
      <button onClick={handle}>Hello</button>
    </div>
  );
}

enter image description here

But if there is a text value already set from the varialbe, It's not showing the value in the alert box. It's empty. As you can see in the below code. I'm setting a const hello with a string and setting it as value for the input. But it's not showing in the popup.

import { useState } from "react";

export default function App() {
  const [value, setValue] = useState("");

  function handle() {
    alert(value);
  }
  return (
    <div className="App">
      <h1>Hello App</h1>
      <input
        type="text"
        onChange={(e) => {
          setValue(e.target.value);
        }}
        value={"hello"}
      />
      <button onClick={handle}>Hello</button>
    </div>
  );
}

enter image description here

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

0

The reason why the alert doesn't display the "correct" state value is because on handle() the value has not change "on time".

Try this

import { useState, useCallback } from "react";

export default function App() {
  const [value, setValue] = useState("");

  const handle = useCallback(() => {
    alert(value);
  }, [value]);

  return (
    <div className="App">
      <h1>Hello App</h1>
      <input
        type="text"
        value={value}
        onChange={(e) => {
          setValue(e.target.value);
        }}
      />
      <button onClick={handle}>Hello</button>
    </div>
  );
}

Why useCallback()?

As per React docs. This hook memoise its internal values, and will only update them when any of the dependancies changes.

The value inside the handle() function scope is not updated, by wrapping the handle function inside a useCallback hook and attaching the value state as a dependency you ensure that handle always updates its internal scoped values/variables, hope that helps.

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