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

166
Visualizações
How to render only the new submission using form in React instead of showing the previous submissions as well?

I was working on a multiple input form where we submit the query, and the submitted query is displayed. After submission, all the fields go back to being blank. If I make another submission for another field, the display shows the new submission, but also shows the value for the previous submission of the previous field. I am not sure if I'm explaining it right. I'm still having a hard time understanding a lot of concepts within react. Here's my Form code:

export default function Form({searchQuery}) {


  const [query, setQuery] = useState({
    firstName: "",
    lastName: "",
  });


  const handleChange = (event) => {
    setQuery({ ...query, [event.target.name]: event.target.value });
    
  };

  const handleSubmit = (event) => {
    event.preventDefault();
    searchQuery(query);

    setQuery({ firstName: "", lastName: "",});
  };

  return (
    <div className="form-container">
      <form onSubmit={handleSubmit}> 
        
        <div>
          <input
            type="text"
            name="firstName"
            placeholder="Search by First Name"
            value={query.firstName}
            onChange={handleChange}
          />
        </div>

        <div>
          <input
            type="text"
            name="lastName"
            placeholder= "Search by Last Name"
            value={query.lastName}
            onChange={handleChange}

          />
       


        <div>
          <button>Search</button>
        </div>
      </form>
    </div>
  );
}

And here's the component that shows displays these queries:

function SubmittedData()
{
 const [query, setQuery] = useState([
        {firstName: "",
        lastName: "",
        }]);

 const searchQuery = (query1) => {
    setQuery([
                {
                    firstName: query1.firstName,
                    lastName: query1.lastName,

                }
 
    ]);
  };


return (
 <div className="App">
 <FormMemberSearch searchQuery ={searchQuery} /> 
 {
              query.map(query =>
                {


return (
 <div>

 {query.firstName}   {query.lastName} 
 
 </div> 
                            )  })
 } 

 </div>
  );
}

export default SubmittedData;

When I submit "Ana" for the first name, and leave the LastName field empty, it just shows Ana. If I enter Celly for the first name, it will only show Celly, which is fine. But after that submission, if I enter something like "Delvey" for the last name and submit the form, the result will have Celly Delvey. How do I get it to show only Delvey instead of the value of another field from the previous submission?

Thank you.

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