react native - react context like icon action

前端 未结 1 1336
鱼传尺愫
鱼传尺愫 2021-01-28 13:59

I am currently storing my user using react context, each user can like as many posts as they want.

i have a parameter called isLiked in my backend that can either be true

相关标签:
1条回答
  • 2021-01-28 14:33

    Assuming you are getting the props from parent component.

    const Heart = ({ isLiked }) => {
             
             const [ liked, setLiked ] = useState(false);
           
             useEffect(() => {
                 setLiked(isLiked)
             },[isLiked])
    
             ......
    }
    

    use useEffect to make sure you update your state whenever the isLiked prop changes.

    Then in your parents component.

    const ListofPost = () => {
    
         const data = fetchTheData(url);
         ....
    
    
         return ( data.map( item => <Heart isLiked={item.isLiked} />) )
    }
    
    0 讨论(0)
提交回复
热议问题