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

91
Visualizações
Only displaying input value after button click in React

I am trying to create a simple React form that, on button click, will display the entered input value in a controlled input element. Specifically, I do NOT want to have an identical solution to that in the React docs (constantly updating the displayed value on input change), rather I only want it to update the displayed paragraph text after the user has hit the submit button. I am able to do this with this current solution (conditionally rendering based on submitted state that is set in handler functions):

import { useState } from 'react';

export default function App() {
  const [text, setText] = useState('');
  const [submitted, setSubmitted] = useState(false);

  const handleSubmit = e => {
    e.preventDefault();
    setSubmitted(true);
  };

  const handleChange = e => {
    setSubmitted(false);
    setText(e.target.value);
  };

  return (
    <>
      <form onSubmit={e => handleSubmit(e)}>
        <label>Text: </label>
        <input type="text" value={text} onChange={e => handleChange(e)} />
        <button type="submit" onClick={handleSubmit}>
          Show
        </button>
        {submitted && <p>{text}</p>}
      </form>
    </>
  );
}

But I am guessing that there is a much better way to do this.

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

0

If you want to be able to submit multiple times and keep the last before submitting again, use this:

import { useState } from 'react';

export default function App() {
  const [text, setText] = useState('');
  const [displayText, setDisplayText] = useState('');

  const handleSubmit = e => {
    e.preventDefault();
    setDisplayText(text);
  };

  const handleChange = e => {
    setText(e.target.value);
  };

  return (
    <>
      <form onSubmit={e => handleSubmit(e)}>
        <label>Text: </label>
        <input type="text" value={text} onChange={e => handleChange(e)} />
        <button type="submit" onClick={handleSubmit}>
          Show
        </button>
        {displayText && <p>{displayText}</p>}
      </form>
    </>
  );
}

displayText && ... is so that the paragraph tag doesn't exist until it has a value to display, but you can just replace that section with out that and it will work.

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