React Native onPress being called automatically

React Native-Android

React Native-Android Problem Overview


I am having trouble with react-native onPress Feature. The onPress should only work when it is actually been triggered by a touch event (i suppose) , that is when i press the button on the screen. But it seems the onPress gets triggered itself when the render function is called. When i try to press manually, it doesn't work.

  import React, { Component } from 'react';
  import { PropTypes, Text, View ,Alert } from 'react-native';
  import { Button } from 'react-native-material-design';

export default class Home extends Component {
  
  render() {
    return (
          <View style={{flex:1}}>
            <Button value="Contacts" raised={true} onPress={this.handleRoute('x')} />
            <Button value="Contacts" raised={true} onPress={this.handleRoute('y')} />
            <Button value="Contacts" raised={true} onPress={this.handleRoute('z')} />
          </View>
          );
}
handleRoute(route){
  alert(route) // >> x , y, z 
    }
}

  module.exports = Home;

What am i missing ? Is there something wrong with the way i have assigned or this is some bug ? Any suggestion is highly appreciated.

Video

React Native-Android Solutions


Solution 1 - React Native-Android

try to change

> onPress={this.handleRoute('x')} // in this case handleRoute function is called as soon as render happen

to

> onPress={() => this.handleRoute.bind('x')} //in this case handleRoute doesn't called as soon as render happen

Solution 2 - React Native-Android

You can change to this:

onPress={this.handleRoute.bind(this, 'x')}

or this:

onPress={() => this.handleRoute('x')}

The reason is that onPress takes a function as an argument. In your code, you are calling the function and returning the result immediately (when render is called) rather than referencing the function for React to call later on the press event.

The reason you need the bind(this) is because the function loses it's bound instance when you just do (this.handleRoute) and you have to tell it which this to use. The bind function takes the other arguments to call on the function later. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind for more descriptive info on bind.

There is another way in which you can bind in the constructor. You can read about ways to handle this in React here: https://facebook.github.io/react/docs/handling-events.html

Solution 3 - React Native-Android

onPress={this.handleevent.bind(this, 'A')}

or use this:

onPress={() => this.handleevent('B')}

Solution 4 - React Native-Android

Change

onPress={this.handleRoute('x')}

to

onPress={()=>this.handleRoute('x')}

Otherwise, the function gets invoked as soon as the render method gets called.

Solution 5 - React Native-Android

The reason for such behaviour is on every render, reference to the function is created.

So, to avoid that, use bind function OR arrow function to call on onPress

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSijan ShresthaView Question on Stackoverflow
Solution 1 - React Native-AndroidManjeet SinghView Answer on Stackoverflow
Solution 2 - React Native-AndroidbhelxView Answer on Stackoverflow
Solution 3 - React Native-AndroidShivo'ham 0View Answer on Stackoverflow
Solution 4 - React Native-AndroidPumuckeloView Answer on Stackoverflow
Solution 5 - React Native-AndroidKushal DesaiView Answer on Stackoverflow