So I have some data I would like to cycle through:
const data = {
names: [
{
name: "Jordan"
// additional data
},
{
name: "Holly"
// additional data
},
{
name: "Sean"
// additional data
}
]
};
Using useState
hook, I start my data from the index of 0:
const [index, setIndex] = useState(data.names[0]);
How can I cycle through the objects in my array using state? I have created a button that calls handleClick
, how can I +1 through the array?
function App() {
const [index, setIndex] = useState(data.names[0]);
function handleClick() {
setIndex(); // Help!
}
return (
<div className="App">
<h1>{index.name}</h1>
<button onClick={handleClick}>Change name</button>
</div>
);
}