Akila

Akila

  • 2k
  • 117
  • 4.8k

Changing state for pressed item only

Nov 26 2021 10:27 AM

Hi 

i am using like button in flatlist.but if i click one post like button.then all like button will change.Is there any option for change selected post like button only 

const App = ({item, index}) => {

 

  const [like, setLike] = useState({});

  const [color, setColor] = useState("green");

 

  const toggleLike = () => {

   setLike(!like)

   if(like) {

    setColor("red");

   } else {

    setColor("green");

   }

  }

  

  return (

   <View style={{marginTop:200}}>

           <TouchableWithoutFeedback onPress={toggleLike}>

        <Icon name='heart' type='FontAwesome' size={32} style={{color, paddingRight: 10 }}/>

      </TouchableWithoutFeedback>

      <TouchableWithoutFeedback onPress={toggleLike}>

        <Icon name='heart' type='FontAwesome' size={32} style={{color, paddingRight: 10 }}/>

      </TouchableWithoutFeedback>

 

    </View>

   );

 }