selvi jp

selvi jp

  • NA
  • 323
  • 77.3k

onclick change image react native

Nov 17 2021 9:24 AM

Hi

I want to change image when the image  is pressed.

const white = require('./images/white.png')

const black = require('./images/black.png')

const shirts = { white, black }

const Shirts = () => { const [selected, setSelected] = useState(shirts.white)

return ( < >

< img src = { selected } alt = 'shirt' / >

< button onClick = { () => setSelected(shirts.black) } > Click < /button> < /> ) }

Its working but i am using list of 10 image.i want if i click one image then one image will only change.But here if i click one image all image will change .how can i fix this


Answers (2)