Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to update object onClick in Rect?

I’m trying to make vote system, but unfortunetly i can’t make to update vote value with every click? It’s working only once. Data is from data.json file.

  const [vote, setVote] = useState<number>(props.score);

  const voteUp = () => {
    setVote(props.score + 1);
  };

  const voteDown = () => {
    setVote(props.score - 1);
  };

  return (
    <CommentStyled key={props.id}>
      <div>
        <CommentScore>
          <VoteButtonStyled onClick={voteUp}>
            <img src={IconPlus} alt="IconPlus" />
          </VoteButtonStyled>
          {vote}
          <VoteButtonStyled onClick={voteDown}>
            <img src={IconMinus} alt="IconMinus" />
          </VoteButtonStyled>
        </CommentScore>
      </div>

Here’s my project on github: https://github.com/xflameyoke/interactive-comment-section-app

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are updating based on props.score, which is propably fixed. Update based on the vote instead:

const voteUp = () => {
    setVote(lastVote => lastVote + 1);
 };

const voteDown = () => {
    setVote(lastVote => lastVote - 1);
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading