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

125
Views
Problema para mostrar un contador de Me gusta en el front-end en React JS (pero funciona en la API)

primera vez que publico aqui!

Estamos trabajando en un proyecto de grupo y tenemos un problema en el incremento de un artículo en nuestro front-end. De momento podemos comprobar en nuestro console.log que el número va subiendo al hacer clic en el botón.

Eso sí, tenemos que refrescar la página para ver los nuevos "me gusta".

Así que aquí está el código en nuestro componente.

 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

Necesitas un estado aquí para administrar los Me gusta.

Algo como

 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) };

luego, en la interfaz de usuario, puede representarlo así

 <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!