import {useState} from 'react';
export default function App() {
const [names, setNames] = useState(['Alice', 'Bob']);
const handleClick = () => {
// 👇️ push to end of state array
setNames(current => [...current, 'Carl']);
// 👇️ spread an array into the state array
// setNames(current => [...current, ...['Carl', 'Delilah']]);
// 👇️ push to beginning of state array
// setNames(current => ['Zoey', ...current]);
};
return (
<div>
<div>
<button onClick={handleClick}>Push to state array</button>
</div>
{names.map((element, index) => {
return (
<div key={index}>
<h2>{element}</h2>
</div>
);
})}
</div>
);
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter