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.

Getting the OnPress source element on SVGs with hundreds of shapes.

See original GitHub issue

I have a lot of “ready to go” SVGs that I convert with svgr to components. Inside the SVG there are many paths, each one has a fixed Id that cannot be changed (it is used in other apps).

Q: I need to know which shape the user has touched … looks simple

In my PoC the only way I found is to modify the component by svgr by adding these attributes for each path (which are hundreds):

ref={this.circleRef} onPress={e => this.onCirclePress(e, this.circleRef.current.props.id)}

I know arrow functions are bad in render, but I need the onPress location coordinates as well.

The onPress event gives me target (runtime id) which is not available from the DOM and therefore I can’t associate it to my fixed Id.

Is there really no solution to get the source element pressed by the user? Dropping RN for this would be a pity.

Thank you in advance

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:38 (18 by maintainers)

github_iconTop GitHub Comments

1reaction
msandcommented, Apr 3, 2020

Looks good imo, explicit, clear, slightly repetitive perhaps, but quite much required for typescript, probably not a significant contribution to total app size, and easy to add further tailor-made helpers and convenience methods per component type. Pull request looks good as well, will merge once I have some spare time to prepare a release. At least no reason to suspect anything breaking in this case what I can see.

1reaction
msandcommented, Apr 2, 2020

Something like this might help with the types for the id prop:

import * as React from 'react';
import { Text, View, StyleSheet, GestureResponderEvent } from 'react-native';
import * as RNSVG from 'react-native-svg';

class Circle extends React.PureComponent<
  {
    id: string;
    onPress: (e: GestureResponderEvent, id: string) => void;
  } & RNSVG.Circle
> {
  onPress = (e: GestureResponderEvent) => {
    const { onPress, id } = this.props;
    if (onPress) {
      onPress(e, id);
    }
  };
  render() {
    const { onPress, props } = this;
    const { onPress: _onPress, ...rest } = props;
    // @ts-ignore
    return <RNSVG.Circle onPress={onPress} {...rest} />;
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting the OnPress source element on SVGs with hundreds ...
I have a lot of "ready to go" SVGs that I convert with svgr to components. Inside the SVG there are many paths,...
Read more >
Making an svg image object clickable with onclick, avoiding ...
So, I made a div exclusively for the svg, and placed onclick in this div tag. But, no effect unless visitor clicks on...
Read more >
How to use SVGs in React | Sanity.io guide
This article will explore how to use SVG in React in three examples.
Read more >
Work with SVG format in Illustrator - Adobe Support
SVG, on the other hand, is a vector format that describes images as shapes, paths, text, and filter effects. The resulting files are...
Read more >
High Performance SVGs | CSS-Tricks
This doesn't mean that you can't make seemingly complex shapes. But hundreds of path points can sometimes have the same appearance and interest ......
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