How to update an array of objects with clickHandler and useState in Reactjs?
import React, { useState } from 'react';
const TextVote = () => {
const [votes, setVotes] = useState([
{ text: 'OneOneOne', vote: 0 },
{ text: 'TwoTwoTwo', vote: 5 },
{ text: 'ThreeThreeThree', vote: 0 }
]);
const votesHandler = () => {
const newClicks = {
...votes,
vote: votes[1].vote + 1
};
setVotes(newClicks);
};
return (
<div className='box'>
<div>{votes[1].text}</div>
<div>
<button onClick={votesHandler}>vote</button>
<div>{votes[1].vote}</div>
</div>
</div>
);
};
export default TextVote;
Want to know how to update the array and some feedback after the clicks of the updates. Right now it should update just one object, but it isn't. The basic idea is to vote for comments and return the comment with the most votes. It is a mock-up just to get ideas how it could work. I don't have the intention to render the whole array. Just the one with the most votes.