Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use onPress on a custom component?

Tags:

Let's assume I have this custom component:

export default class Button extends Component {
  render(){
    return(
      <TouchOpacity>
        <Text> Button </Text>
      </TouchOpacity>
    )
  }
}

And I use it in a parent component like this :

export default class MainPage extends Component {
  render(){
    return(
      <Button onPress={ this.doSomething }></Button>
    )
  }
}

For some reason (unknown to me at least) this onPress even won't happen. I'm pretty new to react-native btw. I believe I must find a way to enable this kind of event handling.

Is it possible to get a small example how to achieve that based on my examples above ?

like image 903
Nick Avatar asked Aug 19 '16 11:08

Nick


People also ask

Can we add onPress on view in React Native?

React Native onPress on a ViewYou can do this using onStartShouldSetResponder (This will work only after React Native version -> 55.3).

How do I make a component clickable in React Native?

In React Native, TouchableOpacity is used to making any component clickable or touchable. By using this component, we can make any component perform any action or task on click using the onPress method.

How do you write onPress in React Native?

The onPress event is called when user touch or press the Text component. To Set onPress onClick on Text in React Native we have to use onPress={} prop in react native. The onPress event is supported by both Android and iOS platforms.

How do you handle the long press in React Native?

These long presses can be handled by passing a function to the onLongPress props of any of the "Touchable" components.


2 Answers

So, just found out how to handle this.

export default class Button extends Component {
  constructor(props){
    super(props)
  }
  render(){
    return(
      <TouchableOpacity
      onPress={this.props.onPress}
      >
        <Text> Button </Text>
      </TouchableOpacity>
    )
  }
}

and

export default class MainPage extends Component {
  render(){
    return(
      <Button onPress={ this.doSomething }></Button>
    )
  }
}

Long story short: since the onPress I'm passing is a prop in the MainPage, I'm passing it a function (this.doSomething) which later on is activated in Button's onPress.

like image 93
Nick Avatar answered Sep 28 '22 03:09

Nick


Nick's answer is right.Along with that if you want to call separate function for both child and parent component then you can use onPressOut. This will help to manage state of componant

export default class Button extends Component {
  constructor(props){
    super(props)
  }

onClickListen = () => {
        if (this.state.isSelected === false) {
            this.setState({isSelected:true});
            isSelectedPass=true;
        }
        else {
            this.setState({isSelected:false});
            isSelectedPass=false;

        }
    }

  render(){
    return(
      <TouchableOpacity
      onPress={this.props.onPress} onPressOut={this.onClickListen}
      >
        <Text> Button </Text>
      </TouchableOpacity>
    )
  }
}

export default class MainPage extends Component {
  render(){
    return(
      <Button onPress={ ()=>this.doSomething }></Button>
    )
  }
}
like image 30
Rajesh Nasit Avatar answered Sep 28 '22 03:09

Rajesh Nasit