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

114
Views
how to do Like button implementation?

I'm using localstorage for storing user id and then sending it to state, then using it to hide like button after clicking it, but after clicking like button, it doesn't disappearing and user can keep liking the post(many times).

Login code: Here I store the user in localstorage and dispatch it.

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

userReducer code:

export const initialState = null

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

Like functionality:

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

frontend code:

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

The main problem is in this above code only, it takes state._id, but only shows like button after clicking also. I have checked state, it does contain _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!