React在用户点击“喜欢”按钮时,无需刷新页面即可更新“喜欢”计数器。

huangapple go评论72阅读模式
英文:

React updating Like counter when user clicks like button without refreshing page

问题

Sure, here is the translated code:

// 这是我的父组件,用于渲染我的“喜欢组件”。父组件有一个名为“Likes”的状态,用于存储所有的“喜欢”。从“喜欢”组件调用“handleLike”函数。“likeFood(foodId, UserId)”是我的 API 调用,用于为特定帖子创建新的喜欢,我将其保存在一个单独的文件中。

// 这是我的 API
export default function likeFood(foodId, UserId) {
  Axios.post(`${process.env.REACT_APP_APIENDPOINT}/Likes/like`, {
    foodId: foodId,
    UserId: UserId,
  }).catch(function (error) {
    console.log(error);
  });
}

// 这个“handleLike”函数从子“喜欢”组件调用,还应该处理更新状态。
handleLike = async (foodId, UserId) => {
  const oldLikes = this.state.Likes;
  const newLikes = oldLikes.filter((like) => like.foodId === foodId);
  likeFood(foodId, UserId);
  this.setState({ Likes: newLikes });
};

// 请注意,如果我将“const newLikes = oldLikes.filter((like) => like.foodId === foodId);”更改为
// “const newLikes = oldLikes.filter((like) => like.foodId !== foodId);”,
// 在不喜欢时状态更新正常,但喜欢时不正常。

// 在这里,我渲染了我的“喜欢”组件
<Like
  foodId={food.id}
  UserId={id}
  Likes={this.state.Likes}
  handleLike={this.handleLike}
/>;

// 这是我的“喜欢”组件
function Like({ foodId, UserId, Likes, handleLike }) {
  // 这个函数计算每个特定帖子的喜欢数量
  function likeAmount(foodId) {
    const likes = Likes.filter((like) => like.foodId === foodId);
    return likes.length;
  }

  return (
    <div className="like-button">
      <BsHeart
        className="like"
        size={24}
        onClick={() => handleLike(foodId, UserId)}
      />
      <p>{likeAmount(foodId)}</p>
    </div>
  );
}

export default Like;

Please note that I've only translated the code and left out the comments and explanations. If you have any specific questions or need further assistance, please feel free to ask.

英文:

> This is my parent component that renders my Like Component. The parent component has a state called Likes that stores all the Likes. The handleLike function gets called from the Like component. likeFood(foodId, UserId) is my api call that creates a new like for specific post that i have in a separate file.

   // this is my api 
export default function likeFood(foodId, UserId) {
Axios.post(`${process.env.REACT_APP_APIENDPOINT}/Likes/like`, {
foodId: foodId,
UserId: UserId,
}).catch(function (error) {
console.log(error);
});
}
// this handleLike function gets called from the child Like component and should also handle updating the state.
handleLike = async (foodId, UserId) =&gt; {
const oldLikes = this.state.Likes;
const newLikes = oldLikes.filter((like) =&gt; like.foodId === foodId);
likeFood(foodId, UserId);
this.setState({ Likes: newLikes });
};

>Note if i change const newLikes = oldLikes.filter((like) => like.foodId === foodId);

to

>const newLikes = oldLikes.filter((like) => like.foodId !== foodId);

the state updates properly when disliking but not when liking.

     // here i render my Like component
&lt;Like
foodId={food.id}
UserId={id}
Likes={this.state.Likes}
handleLike={this.handleLike}
/&gt;

> This is my Like component

     function Like({ foodId, UserId, Likes, handleLike }) {
// this function calculates the amount of likes for each specific post
function likeAmount(foodId) {
const likes = Likes.filter((like) =&gt; like.foodId === foodId);
return likes.length;
}
return (
&lt;div className=&quot;like-button&quot;&gt;
&lt;BsHeart
className=&quot;like&quot;
size={24}
onClick={() =&gt; handleLike(foodId, UserId)}
/&gt;
&lt;p&gt;{likeAmount(foodId)}&lt;/p&gt;
&lt;/div&gt;
);
}
export default Like;

答案1

得分: 1

你需要改变喜欢的方法。

handleLike = async (foodId, UserId) => {
  const oldLikes = this.state.Likes;
  const newLikes = [...oldLikes, {foodId: foodId}]; // 定义你的对象
  likeFood(foodId, UserId);
  this.setState({ Likes: newLikes });
};
英文:

You need to change liking method.

handleLike = async (foodId, UserId) =&gt; {
const oldLikes = this.state.Likes;
const newLikes = [...oldLikes, {foodId: foodId}]; // Define your object
likeFood(foodId, UserId);
this.setState({ Likes: newLikes });
};

huangapple
  • 本文由 发表于 2023年7月11日 03:22:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定