Gregor Samsa
Gregor Samsa

Reputation: 115

Alert appears before click or event

I have this line in my React Native app :

   <Image onPress={alert('Image')} source={require('./assets/home.png')} />

I want it to show an alert after clicking on the image, the problem is the alert appears after the login without even clicking on the Image or anything.

Upvotes: 1

Views: 82

Answers (1)

Spencer Carli
Spencer Carli

Reputation: 696

As it stands, and as you've found, your function will be called immediately. To get the interaction you want you need to use the following

<Image onPress={() => alert('Image')} source={require('./assets/home.png')} />

Upvotes: 4

Related Questions