Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onclick function is not working in react native application

I'm new to react native. I wrote the following code to call a function, if a user clicks on a text.

var login = React.createClass({      openPopup: function(){         console.log("function called");      },      render: function(){         return (            <View onClick={this.openPopup}>               <Text>                  Login               </Text>            </View>         );      } }); 

Is there anything wrong in the above code? If I click the login text, I'm not getting any feedback in the console.

EDIT This question is react native specific. Not a duplicate of any other question in Stack Overflow.

like image 338
Sriraman Avatar asked Dec 31 '15 08:12

Sriraman


People also ask

How do I get a click event in react native?

The React onClick event handler enables you to call a function and trigger an action when a user clicks an element, such as a button, in your app. Event names are written in camelCase, so the onclick event is written as onClick in a React app. In addition, React event handlers appear inside curly braces.

How do you write onClick function in Reactjs?

React events are written in camelCase syntax: onClick instead of onclick . React event handlers are written inside curly braces: onClick={shoot} instead of onClick="shoot()" .

Can we use onClick in Div tag react?

To set your mind at ease, the onClick event does work with divs in react, so double-check your code syntax.


1 Answers

Try this-

<TouchableOpacity onPress={this.openPopup}>      <View> <Text>...</Text> </View>  </TouchableOpacity> 
like image 58
hazardous Avatar answered Oct 03 '22 07:10

hazardous