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

123
Views
¿Cómo hacer la implementación del botón Me gusta?

Estoy usando el almacenamiento local para almacenar la identificación del usuario y luego enviarlo al estado, luego usarlo para ocultar el botón Me gusta después de hacer clic en él, pero después de hacer clic en el botón Me gusta, no desaparece y al usuario le puede seguir gustando la publicación (muchas veces).

Código de inicio de sesión: aquí almaceno al usuario en localstorage y lo envío.

 localStorage.setItem("user",JSON.stringify(data.user)) dispatch({type:"USER",payload:data.user}) history.push('/c')

código de usuarioReductor:

 export const initialState = null export const reducer = (state,action)=>{ if(action.type==="USER"){ return action.payload } return state }

Como funcionalidad:

 const likePost = (id)=>{ fetch('/post/like',{ method:"put", headers:{ Accept:"application/json", "Content-Type":"application/json", }, body:JSON.stringify({ postId:id }) }).then(res=>res.json()) .then(result=>{ //console.log(result) const newData = data.map(post=>{ if(post._id===result._id){ return result }else{ return post } }) setdata(newData) }).catch(err=>{ console.log(err) }) }

código de interfaz:

 {posts.likes.includes(state._id) ?<AiTwotoneDislike size="2em" onClick={()=>{unlikePost(posts._id)}}/> :<AiFillLike size="2em" onClick={()=>{likePost(posts._id)}}/> }

El problema principal está solo en este código anterior, toma state._id, pero solo muestra el botón Me gusta después de hacer clic también. He comprobado el estado, contiene _id.

about 4 years ago · Juan Pablo Isaza
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!