Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Issue to show a like counter in the front end in React JS (but it works in the API)

first time I post here !

We're working on a group project and we have an issue in the incrementation of an item in our front-end. At the moment we can check in our console.log that the number is rising when click on the button.

Althought, we have to refresh the page to see the new "like".

So here's the code in our compenent.


import "./Post.css";
import { useEffect, useState } from "react";
import { addLike, createPost } from '../../lib/social-network-library-master';

function Post({post}) {


    const handleLikes = async () => {
        let result = await addLike(post._id)       
    };
    
    return (

        <div id="general-container">
            <h2>{post.title}</h2><br/>
            <p>{post.content}</p><br/>
            <p>Posté par {post.firstname} {post.lastname}</p><br/>
            <div className="boutton-like-container">
            <button className="boutton-like" onClick={() =>handleLikes()}> 
                like
            </button>
            <p>{post.likes.length} likes</p>
            </div>
        </div>
    )
};

export default Post;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need a state here to manage the likes.

Something like

const [counter, setCounter] = useState(0)

const handleLikes = async () => {
  let result = await addLike(post._id);
  //setCounter(prev => prev + 1);
  // if the result is coming from api then instead just set the response that you get
  setCounter(result)
};

then in the UI you can render it like so

<p>{counter}</p>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!