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

260
Visualizações
How to keep track of every state's update to be able to render on a list all the data that has been sent through a form

I am creating an app to keep track of expenses.

Before creating the server side. What I am trying to do is to render all information held in the state in a list format.

As I am not storing this data in a database. I wondered if there would be a way to do it without creating the database. I mean, just do this task dealing just with react.(I have to learn server side yet)

What I've done: I sent the data, data is the state created in , as props to component, and and in there, I created an empty array to push each property of the data object. After created the array, in the return of the List component, I map through that array to print all the information.

But obviously, once I type something, the previous data disappears.

What I am trying to achieve is what I've explained at the beginning.

How to keep track of all inputs and print all of them without losing the previous enter.

I am new to this. So, please, go easy on me :)


export default function Forms() {
    const [data, setData] = useState({
        concept:"",
        amount:"$",
        date:getCurrentDate(),
        select: "Income"
    })
    function handleChange(event){
        let name = event.target.name;
        let value = event.target.value;
        setData({...data,[name]:value})
    
      }


  return (
    <>
      <h2>Incomes - Outcomes</h2>
      <form className="form">
        <label>Concept:</label> 
        <input type="text" name="concept" value={data.concept} onChange={handleChange} />

        <label>Amount:</label>
        <input type="text" name="amount" value={data.amount} onChange={handleChange}/>

        <label>Date:</label>
        <input  name="date" value={data.date} onChange={handleChange}/>

        <label>Select type of expense:</label>
        <select name="select" value={data.type} onChange={handleChange}>
            <option value="Income">Income</option>
            <option value="Outcome">Outcome</option>
        </select>
       

        
      </form>
    <List data={data}/>
    </>
  );
}


export default function List({ data }) {
 

  const arrayOfExpenses = [];
  
  Object.keys(data).forEach((key) => {
    arrayOfExpenses.push(key + ": " + data[key]);
  })

    function handleSubmit(event) {
      event.preventDefault();
    }



  return (
    <div>
      <button onClick={handleSubmit}>Submit</button>
    {arrayOfExpenses.map((element, index) =>(
        <ul key={index}>
            <li>{element}</li>
        </ul>
    )
        
    )}
    </div>
  );
}
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