react native - react context like icon action

前端 未结 1 1338
鱼传尺愫
鱼传尺愫 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 => ) )
    }
    

    0 讨论(0)
提交回复
热议问题