Home > database >  How to update object onClick in Rect?
How to update object onClick in Rect?

Time:07-25

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

CodePudding user response:

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);
};
  • Related