List with useState in the React playground
Paste into App.js. Each click creates a new array instead of mutating the old one, which is what lets React detect the change and re-render.
import { useState } from 'react';
export default function App() {
const [items, setItems] = useState(['Read the React docs']);
const [text, setText] = useState('');
const addItem = () => {
if (!text.trim()) return;
setItems([...items, text.trim()]);
setText('');
};
return (
<div>
<input value={text} onChange={(e) => setText(e.target.value)} placeholder="New item" />
<button onClick={addItem}>Add</button>
<p>{items.length} item(s)</p>
<ul>
{items.map((item, i) => <li key={i}>{item}</li>)}
</ul>
</div>
);
}