Skip to content

fix: use immutable update for comment vote state#1546

Open
GeoSyntax wants to merge 1 commit into
apache:mainfrom
GeoSyntax:fix/comment-vote-immutable-update
Open

fix: use immutable update for comment vote state#1546
GeoSyntax wants to merge 1 commit into
apache:mainfrom
GeoSyntax:fix/comment-vote-immutable-update

Conversation

@GeoSyntax

@GeoSyntax GeoSyntax commented Jun 22, 2026

Copy link
Copy Markdown

What changed

  • Replace direct object mutation with spread operator in submitVoteComment for immutable state update

Why

  • The current code directly mutates the item object inside comments.map(), which violates React's immutable update principle
  • The child component ActionBar is wrapped with React.memo, direct mutation may prevent memo from detecting changes, causing potential rendering issues

Before

item.vote_count = is_cancel ? item.vote_count - 1 : item.vote_count + 1;
item.is_vote = !is_cancel;
return item;

After

return {
  ...item,
  vote_count: is_cancel ? item.vote_count - 1 : item.vote_count + 1,
  is_vote: !is_cancel,
};

Testing

  • Vote / unvote on comments works correctly
  • Vote after pagination works correctly
  • Data is correct after page refresh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant