Actually, I am stuck at a point, Please have a look the code once. I want to push the user input into my array. Can anyone explain why it's throwing error.
import React, { useState } from 'react';
function Cart() {
const [item, setItem] = useState({ cart: ['Corn', 'Potato'] });
const saveInput = (e) => {
setItem({ input: e.target.value });
};
const addNewItem = () => {
const { cart, input } = item;
cart.push(input);
return setItem({ cart: cart });
};
return (
<div>
<input type="text" onChange={saveInput} />
<button onClick={addNewItem}>Add Item</button>
<ol>
{item.cart.map((subItems, sIndex) => {
return <li key={sIndex}> {subItems}</li>;
})}
</ol>
</div>
);
}
export default Cart;