简体   繁体   English

反应:setState() 钩子,未设置初始颜色

[英]React: setState() hook, initial color not being set

I am trying to set a basic like item, similar to facebook likes to be a color of black when the page first renders, and then using React hooks, useState() and useEffect() to change the color when the state of likes changes.我正在尝试设置一个基本的喜欢项目,类似于 facebook 喜欢在页面首次呈现时为黑色,然后使用 React 钩子、useState() 和 useEffect() 在喜欢的状态发生变化时更改颜色。 However, I have got the color to render, but it is also rendering on load so not changing from black to green when a user clicks "like" and changes the like state.但是,我已经获得了要渲染的颜色,但它也在加载时渲染,因此当用户单击“喜欢”并更改喜欢状态时不会从黑色变为绿色。

import React, { useState, useEffect } from "react";

function Update() {
  **const [like, updateLikes] = useState(0);
  const [dislike, updateDislikes] = useState(0);
  const [likeColor, setLikeColor] = useState("black");
  const [dislikeColor, setDislikeColor] = useState("black");
  useEffect(() => {
    setLikeColor("green");
  }, [like]);
  useEffect(() => {
    setDislikeColor("red");
  }, [dislike]);**

  return (
    <div className="updateDiv">
      <span className="update">
        <img className="profilePic" src="https://picsum.photos/50/50" alt="" />
        <div className="name">Username</div>
        <div className="time">08:30am</div>
        <div className="text">Reading a good book!</div>
        <div className="likes">
          Likes:
          **<p style={{ color: likeColor }}>{like}</p>**
        </div>
        <div className="dislikes">
          Dislikes:
          <p style={{ color: dislikeColor }}>{dislike}</p>
        </div>
        <button type="submit" onClick={() => updateLikes(like + 1)}>
          Like
        </button>
        <button type="submit" onClick={() => updateDislikes(dislike + 1)}>
          Dislike
        </button>
        <hr />
      </span>
    </div>
  );
}

export default Update;

So, I want the initial color of the like <p> element to be black when the page is first rendered, and then the useEffect methods to be called to change the color of said element when the state of "like" changes, but at the moment the color is being set when the page renders, so it isn't being initialised as black.所以,我希望在第一次渲染页面时,like <p>元素的初始颜色为黑色,然后当“like”的状态改变时调用 useEffect 方法来改变所述元素的颜色,但是在在页面呈现时设置颜色的那一刻,所以它没有被初始化为黑色。

Probably a quick fix, but I'm starting to bang my head against the wall :D可能是一个快速修复,但我开始用头撞墙:D

The useEffect hook will also run on mount. useEffect 钩子也将在挂载时运行。 So you need to prevent the hook from setting the color on the first render.所以你需要防止钩子在第一次渲染时设置颜色。

You can do this by using useRef hook.您可以通过使用 useRef 钩子来做到这一点。

const mountRef = useRef(false);

useEffect(() => {
  if (mountRef.current) {
    setLikeColor("green");
  }
  else {
   mountRef.current = true;
  }     
}, like);

Because Your are calling setLikeColor and setDislikeColor under useEffect hook and initially useEffect is running and change the color.You can do like below :因为您正在 useEffect 挂钩下调用 setLikeColor 和 setDislikeColor 并且最初 useEffect 正在运行并更改颜色。您可以执行以下操作:

 import React, { useState, useEffect } from "react"; function Update() { const [like, updateLikes] = useState(0); const [dislike, updateDislikes] = useState(0); const [likeColor, setLikeColor] = useState("black"); const [dislikeColor, setDislikeColor] = useState("black"); useEffect(() => { console.log("useEffect"); if (like > 0) { setLikeColor("green"); } }, [like]); useEffect(() => { if (dislike > 0) { setDislikeColor("red"); } }, [dislike]); return ( <div className="updateDiv"> <span className="update"> <img className="profilePic" src="https://picsum.photos/50/50" alt="" /> <div className="name">Username</div> <div className="time">08:30am</div> <div className="text">Reading a good book!</div> <div className="likes"> Likes:<p style={{ color: likeColor }}>{like}</p> </div> <div className="dislikes"> Dislikes: <p style={{ color: dislikeColor }}>{dislike}</p> </div> <button type="submit" onClick={() => updateLikes(like + 1)}> Like </button> <button type="submit" onClick={() => updateDislikes(dislike + 1)}> Dislike </button> <hr /> </span> </div> ); } export default Update;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM