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

209
Visualizações
State is of my react app is not changing after clicking the button

I am trying to save the input from a form inside an array using useState. I created a function for clicking an Add button, but the onChange input is not added to the notes array when I click the button. Empty array in the console log. Please be kind enough to point out the problem.

const {useState} = React;

function CreateArea() {
  const [newNote, setNote] = useState({
    title: "",
    content: "",
  });

  const [notes, setNotes] = useState([]);
  console.log(notes);

  function addNote() {
    setNotes((prevNote) => {
      return [...prevNote, newNote];
    });
  }

  function handleChange(event) {
    const { value, name } = event.target;
    setNote((prevValue) => {
      return {
        ...prevValue,
        [name]: value,
      };
    });
  }

  return (
    <div>
      <form>
        <input
          onChange={handleChange}
          name="title"
          placeholder="Title"
          value={newNote.newTitle}
        />
        <textarea
          onChange={handleChange}
          name="content"
          placeholder="Take a note..."
          value={newNote.newContent}
          rows="3"
        />
        <button onClick={addNote}>Add</button>
      </form>
    </div>
  );
}


ReactDOM.render(<CreateArea />, document.getElementById("root"));
<div id="root"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script>

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