0

I had in reactjs

render() {
  return (
    <div
      onMouseDown={this.onMouseDown}
      onMousUp={this.onMouseUp}
    >
    </div>
  )
}

For the purpose of running some code while the user was holding down the element.

How can I do the same thing in react-native? Simply turning div to View and keeping onMouseDown and onMouseUp doesn't do anything on either event, and this that I found: https://stackoverflow.com/a/44984892/3310334

<View onStartShouldSetResponder={() => true}
      onResponderStart={calledWhenTheTouchStarts}>
    <Text>Press Me!</Text>
</View>

Looks like unintentional misusage of an API

theonlygusti
  • 11,032
  • 11
  • 64
  • 119
  • Use [TouchEvent](https://developer.mozilla.org/en-US/docs/Web/API/Touch_events)s – Randy Casburn Jan 04 '22 at 01:35
  • You could use the `Pressable` API: https://reactnative.dev/docs/pressable and the `onPressIn` and `onPressOut` props – Nick Parsons Jan 04 '22 at 02:05
  • @NickParsons I'm getting this error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. – theonlygusti Jan 04 '22 at 02:10
  • @theonlygusti could you show the code that led to that error? – Nick Parsons Jan 04 '22 at 02:11
  • @NickParsons {}} onPressOut={()=>{}}> – theonlygusti Jan 04 '22 at 02:21
  • @theonlygusti The conents of your pressable component should hold the thing that you want to be pressable: `{}} onPressOut={()=>{}}>Press Me!` (which you may be doing and just omitted for brevity). Also double-check how you are importing the `Pressable` component (and other components) – Nick Parsons Jan 04 '22 at 02:26
  • @NickParsons yeah omitted for brevity, this turned out to be the mrp despite being so simple. It needed the attributes before failing though. importing like `import { View, StyleSheet, Pressable } from 'react-native'` – theonlygusti Jan 04 '22 at 02:32

1 Answers1

1

You’ll want to use a Pressable element instead of View with a combination of onPressIn and onPressOut events. See more here

dansiemens
  • 530
  • 1
  • 4
  • 12