score:1

Accepted answer
 setTransaction((prevTransactions) => [...prevTransactions, trans]);

score:1

  const onSubmit = (e) => {
    e.preventDefault();

    const new_trx = [
      ...transaction,

      {
        text: item,
        amount: parseInt(amount),
        id: Math.floor(Math.random() * 1000)
      }
    ] 

    setTransaction(new_trx);
    setItem("");
    setAmount("");

      Math.sign(amount) === 1
        ? setIncome(prev => prev + parseInt(amount))
        : setExpense(prev => prev - parseInt(amount))

  };

score:1

 const onSubmit = (e) => {
    e.preventDefault();

    setTransaction((prev) => { 
      const newTransaction = [...prev, {
                  text: item,
                  amount: amount,
                  id: Math.floor(Math.random() * 1000)
     }] 

    newTransaction.map((trans) =>
        Math.sign(trans.amount) === 1
          ? setIncome((prev) => prev + Number(trans.amount))
          : setExpense((prev) => prev + Number(trans.amount))
      );
     return newTransaction;
    });

    setItem("");
    setAmount("");
  };

Related Query

More Query from same tag