Reactive Append to Array in React
Let's work with reactively adding elements to an array. For example, let's say we have an array that is output as a list ul:
function App() {
const [notes, setNotes] = useState([1, 2, 3, 4, 5]);
const result = notes.map((note, index) => {
return <li key={index}>{note}</li>;
});
return <div>
<ul>
{result}
</ul>
</div>;
}
Make a button that, when clicked, will add a new element to the end of the array, thereby adding a new li to the end of the ul tag.
Make an input and a button. When the button is clicked, let the input text become a new tag li at the end of the tag ul.