Optimistic rendering to show votes

At the moment the votes {count} is not showing up on mount. I’m using useEffect to get the article and useState to set the count to article.votes. The votes go up after clicking the button twice as the first render doesn’t show the vote count.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const Article = () => {
const { article_id } = useParams();
const [article, setArticle] = useState({ title: "Not Found" });
useEffect(() => {
getArticle(article_id).then((article) => {
setArticle(article);
});
}, []);
const [count, setCount] = useState(article.votes);
function handleCountUp() {
updateArticleVotes(article_id, count, article.votes);
setCount(article.votes++);
}
function handleCountDown() {
setCount(article.votes--);
updateArticleVotes(article_id, count, article.votes);
}
const navigate = useNavigate();
function handleNavigate() {
navigate("./#form");
}
return (
<div>
<div className="one-article-div" id="top">
<h5>Votes: {count}</h5>
<button
onClick={handleCountUp}
name="Vote Up"
className="input-submit margin-left-right"
>
Vote Up
</button>
<button
onClick={handleCountDown}
name="Vote Down"
className="input-submit margin-left-right"
>
Vote Down
</button>
</div>
</div>
);
};
export default Article;
</code>
<code>const Article = () => { const { article_id } = useParams(); const [article, setArticle] = useState({ title: "Not Found" }); useEffect(() => { getArticle(article_id).then((article) => { setArticle(article); }); }, []); const [count, setCount] = useState(article.votes); function handleCountUp() { updateArticleVotes(article_id, count, article.votes); setCount(article.votes++); } function handleCountDown() { setCount(article.votes--); updateArticleVotes(article_id, count, article.votes); } const navigate = useNavigate(); function handleNavigate() { navigate("./#form"); } return ( <div> <div className="one-article-div" id="top"> <h5>Votes: {count}</h5> <button onClick={handleCountUp} name="Vote Up" className="input-submit margin-left-right" > Vote Up </button> <button onClick={handleCountDown} name="Vote Down" className="input-submit margin-left-right" > Vote Down </button> </div> </div> ); }; export default Article; </code>
const Article = () => {
  const { article_id } = useParams();

  const [article, setArticle] = useState({ title: "Not Found" });

  useEffect(() => {
    getArticle(article_id).then((article) => {
      setArticle(article);
    });
  }, []);

  const [count, setCount] = useState(article.votes);

  function handleCountUp() {
    updateArticleVotes(article_id, count, article.votes);
    setCount(article.votes++);
  }

  function handleCountDown() {
    setCount(article.votes--);
    updateArticleVotes(article_id, count, article.votes);
  }

  const navigate = useNavigate();

  function handleNavigate() {
    navigate("./#form");
  }

  return (
    <div>
      <div className="one-article-div" id="top">
        <h5>Votes: {count}</h5>
        <button
          onClick={handleCountUp}
          name="Vote Up"
          className="input-submit margin-left-right"
        >
          Vote Up
        </button>
        <button
          onClick={handleCountDown}
          name="Vote Down"
          className="input-submit margin-left-right"
        >
          Vote Down
        </button>
      </div>
    </div>
  );
};

export default Article;

I’m expecting the <h5>votes: {count}</h5> to show up on mount. At the moment it only shows up after clicking the up vote or down vote.

Inside the useEffect you need to add a setCount(article.votes).

In your case, it doesn’t work because the useEffect run after the mounting of your component. So at that time articles is undefined so votes is also undefined. And the initializer of the count state is set to undefined.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>useEffect(() => {
getArticle(article_id).then((article) => {
setArticle(article);
setCount(article.votes);
});
}, []);
</code>
<code>useEffect(() => { getArticle(article_id).then((article) => { setArticle(article); setCount(article.votes); }); }, []); </code>
useEffect(() => {
    getArticle(article_id).then((article) => {
      setArticle(article);
      setCount(article.votes);
    });
  }, []);

At the moment the votes {count} is not showing up on mount

It would not show up, because here:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> const [count, setCount] = useState(article.votes);
</code>
<code> const [count, setCount] = useState(article.votes); </code>
  const [count, setCount] = useState(article.votes);

you have initialized count with article.votes.
Even if you change article.votes to something else afterwards, the count will not be re-initialized. From the docs:

The value you want the state to be initially. It can be a value of any
type, but there is a special behavior for functions. This argument is
ignored after the initial render.

You have to update count too using setCount, after you get new article in useEffect.

I’m expecting the votes: {count} to show up on mount.

It does display/render on the initial render cycle, but the problem is that the initial article state has an undefined votes property.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const [article, setArticle] = useState({
title: "Not Found" // no votes property
});
const [count, setCount] = useState(article.votes); // undefined
</code>
<code>const [article, setArticle] = useState({ title: "Not Found" // no votes property }); const [count, setCount] = useState(article.votes); // undefined </code>
const [article, setArticle] = useState({
  title: "Not Found" // no votes property
});
const [count, setCount] = useState(article.votes); // undefined

I suggest you provide a default votes property value, and update the effect to also update the count state when it updates the article state. Don’t forget to include the article_id route path parameter as a dependency so the effect also runs if/when the article id parameter value changes.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const [article, setArticle] = useState({
title: "Not Found",
votes: 0,
});
const [count, setCount] = useState(article.votes);
useEffect(() => {
getArticle(article_id)
.then((article) => {
setArticle(article);
setCount(article.votes);
});
}, [article_id]);
</code>
<code>const [article, setArticle] = useState({ title: "Not Found", votes: 0, }); const [count, setCount] = useState(article.votes); useEffect(() => { getArticle(article_id) .then((article) => { setArticle(article); setCount(article.votes); }); }, [article_id]); </code>
const [article, setArticle] = useState({
  title: "Not Found",
  votes: 0,
});
const [count, setCount] = useState(article.votes);

useEffect(() => {
  getArticle(article_id)
    .then((article) => {
      setArticle(article);
      setCount(article.votes);
    });
}, [article_id]);

The vote handlers are also mutating the article state with the post-increment/decrement operators. You should avoid mutating React states. Add or subtract 1 from the current article.votes value and pass that result to the setCount state setter function.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function handleCountUp() {
updateArticleVotes(article_id, count, article.votes);
setCount(article.votes + 1);
}
function handleCountDown() {
updateArticleVotes(article_id, count, article.votes);
setCount(Math.max(0, article.votes - 1));
}
</code>
<code>function handleCountUp() { updateArticleVotes(article_id, count, article.votes); setCount(article.votes + 1); } function handleCountDown() { updateArticleVotes(article_id, count, article.votes); setCount(Math.max(0, article.votes - 1)); } </code>
function handleCountUp() {
  updateArticleVotes(article_id, count, article.votes);
  setCount(article.votes + 1);
}

function handleCountDown() {
  updateArticleVotes(article_id, count, article.votes);
  setCount(Math.max(0, article.votes - 1));
}

The count state appears to be “derived” state from the article state, and typically is not recommended to also exist as React state. I suggest you update the code to update the article.votes state value directly and avoid the effectively duplicate count state.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const [article, setArticle] = useState({
title: "Not Found",
votes: 0,
});
useEffect(() => {
getArticle(article_id)
.then((article) => {
setArticle(article);
});
}, [article_id]);
...
function handleCountUp() {
updateArticleVotes(article_id, article.votes + 1);
setArticle(article => ({
...article,
votes: article.votes + 1,
}));
}
function handleCountDown() {
updateArticleVotes(article_id, Math.max(0, article.votes - 1);
setArticle(article => ({
...article,
votes: Math.max(0, article.votes - 1),
}));
}
...
return (
<div>
<div className="one-article-div" id="top">
<h5>Votes: {article.votes}</h5>
<button
onClick={handleCountUp}
name="Vote Up"
className="input-submit margin-left-right"
>
Vote Up
</button>
<button
onClick={handleCountDown}
name="Vote Down"
className="input-submit margin-left-right"
>
Vote Down
</button>
</div>
</div>
);
</code>
<code>const [article, setArticle] = useState({ title: "Not Found", votes: 0, }); useEffect(() => { getArticle(article_id) .then((article) => { setArticle(article); }); }, [article_id]); ... function handleCountUp() { updateArticleVotes(article_id, article.votes + 1); setArticle(article => ({ ...article, votes: article.votes + 1, })); } function handleCountDown() { updateArticleVotes(article_id, Math.max(0, article.votes - 1); setArticle(article => ({ ...article, votes: Math.max(0, article.votes - 1), })); } ... return ( <div> <div className="one-article-div" id="top"> <h5>Votes: {article.votes}</h5> <button onClick={handleCountUp} name="Vote Up" className="input-submit margin-left-right" > Vote Up </button> <button onClick={handleCountDown} name="Vote Down" className="input-submit margin-left-right" > Vote Down </button> </div> </div> ); </code>
const [article, setArticle] = useState({
  title: "Not Found",
  votes: 0,
});

useEffect(() => {
  getArticle(article_id)
    .then((article) => {
      setArticle(article);
    });
}, [article_id]);

...

function handleCountUp() {
  updateArticleVotes(article_id, article.votes + 1);
  setArticle(article => ({
    ...article,
    votes: article.votes + 1,
  }));
}

function handleCountDown() {
  updateArticleVotes(article_id, Math.max(0, article.votes - 1);
  setArticle(article => ({
    ...article,
    votes: Math.max(0, article.votes - 1),
  }));
}

...

return (
  <div>
    <div className="one-article-div" id="top">
      <h5>Votes: {article.votes}</h5>
      <button
        onClick={handleCountUp}
        name="Vote Up"
        className="input-submit margin-left-right"
      >
        Vote Up
      </button>
      <button
        onClick={handleCountDown}
        name="Vote Down"
        className="input-submit margin-left-right"
      >
        Vote Down
      </button>
    </div>
  </div>
);

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật