question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Differentiate between onPress, onPressIn and onPressOut

See original GitHub issue

Is this a bug report?

Yes (write your answer here)

Have you read the Bugs section of the Contributing to React Native Guide?

Yes (Write your answer here.)

Environment

react-native -v: 0.46.2 node -v: 6.4.0 npm -v: 3.10.3 yarn --version (if you use Yarn):

Then, specify:

  • Target Platform: iOS
  • Development Operating System: Mac OSX ELC
  • Build tools:

Steps to Reproduce

  <ToucableOpacity
      onPress={()=>console.log('onPress')}
      onPressIn={()=>console.log('onPressIn')}
      onPressOut={()=>console.log('onPressOut')}
      delayPressIn={100}
  />

The onPress event should not fire if onPressIn is triggered. If delayPressIn and delayPressOut passes, only the pressIn and pressOut should be triggered.

(Write your steps here:)

Expected Behavior

Dont fire pressIn

(Write what you thought would happen.)

Actual Behavior

pressIn is fired inspite of holding it.

(Write what happened. Add screenshots!)

Reproducible Demo

(Paste the link to an example project and exact instructions to reproduce the issue.)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
srameshrcommented, Dec 11, 2017

After onPressIn event is fired, i see the onPress event is fired to, when onPressIn and onPress handlers are added on the same <TouchableHighlight>. Basically I was trying to emulate click to capture and press and hold to record and release to stop recording on the same button.

1reaction
mfonchocommented, Aug 19, 2017

This should patch the RNTouchable components. and works as expected. Reproduces the behavior of snapchat or instagram touch and hold shutter botton

import React, { Component, createElement, Children } from 'react';

export default class Touchable extends Component {

        _pressThreshold = 300;

	_pressedIn = null;

	_pressInBubbling = null;

	_onPressIn(event){

		this._pressedIn = Date.now();

		this._pressInBubbling = setTimeout( () => {
			this.props.onPressIn ? this.props.onPressIn(event) : null;
		},0);
	}

	_onPressOut(event){
		clearTimeout(this._pressInBubbling);
		this.props.onPressOut ? this.props.onPressOut(event) : null;
	}

	_onPress(event){
		if( (Date.now() - this._pressedIn) < this._pressThreshold ){
			this.props.onPress ? this.props.onPress(event) : null;
		} 
		this._pressedIn = null;
	}

	getProperty(property, props){
		const prop = props[property];
		delete props[property];
		return  prop;
	}

	bindInteractions(props){
		return {
			...props,
			onPress:this._onPress.bind(this),
			onPressIn:this._onPressIn.bind(this),
			onPressOut:this._onPressOut.bind(this),
		}
	}

	render(){
		const props = this.props;
		const children = this.getProperty('children', props);
		const component = this.getProperty('as', props);
		const newProps = this.bindInteractions(props);
		return createElement(component, newProps, Children.toArray(children));
	}
}

Implementation

  <Touchable
      as={TouchableOpacity}
      onPress={()=>console.log('onPress')}
      onPressIn={()=>console.log('onPressIn')}
      onPressOut={()=>console.log('onPressOut')}
      delayPressIn={100}
  />
Read more comments on GitHub >

github_iconTop Results From Across the Web

react native onpressin and onpressout - Stack Overflow
I tried to create a long press button to keep counting, however the interval wont stop when I release the onPressOut
Read more >
Example of onPressIn onPressOut in Pressable React Native
onPressIn :- It calls immediately when user touch the Pressable but before onPress and onPressOut. · onPressOut :- It calls when user release...
Read more >
React Native touchable vs. pressable components
Learn the difference between touchable and pressable components in ... the onPress , onPressIn , onPressOut , and onLongPress callbacks.
Read more >
Why We Should Use Pressable In React Native
As a React Native developer, you've probably visited the ... onPressIn — is activated, after pressing onPressIn → onPressOut — onPress ...
Read more >
Pressable vs. Touchable in React Native | by MahYar - Medium
delayLongPress: Duration in milliseconds from onPressIn by the time ... of the view which a touch is considered a press, before onPressOut ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found