3

I'm working with react-native-svg, as documentation says it's possible to trigger onPress event on svg elements but it doesn't work.

<Svg width={370} height={180} {...props} viewBox="0 0 385 185" >
   <Path
      d="M5.607 13.536l9.267 9.267a2.5 2.5 0 0 1-3.535 3.536L.732 15.732a2.497 2.497 0 0 1-.695-2.196 2.497 2.497 0 0 1 .695-2.197L11.34.732a2.5 2.5 0 0 1 3.535 3.536l-9.267 9.268z"
      fill="white"
      onPress={() => alert('Press on Circle')}
    />
</Svg>

Then I tried the example written in the documentation:

<Svg
            height="100"
            width="100"
        >
    <Circle
    cx="50%"
    cy="50%"
    r="38%"
    fill="red"
    onPress={() => alert('Press on Circle')}
/>
  </Svg>

But as the first one it doesn't work

Emanuele Sacco
  • 411
  • 5
  • 20

1 Answers1

-2

The problem was caused by panResponder as it had higher priority then the button, so the solution is to not return always true from onMoveShouldSetPanResponderCapture. ex:

componentWillMount() {
        this.panResponder = PanResponder.create({
            onMoveShouldSetPanResponderCapture: this.onShouldDrag,
            onPanResponderMove: Animated.event(
            [null, { dx: this.state.pan.x }]
            ),
            onPanResponderRelease: this.onReleaseItem,
            onPanResponderTerminate: this.onReleaseItem,
        });
    }

    onShouldDrag = (e, gesture) => {
        const { dx } = gesture;
        return Math.abs(dx) > 2;
    }
Emanuele Sacco
  • 411
  • 5
  • 20
  • What if I don't have panResponder onMoveShouldSetPanResponderCapture? I don't think you explained solution pretty well. – Nick Nov 11 '20 at 13:25