react-native-svg icon indicating copy to clipboard operation
react-native-svg copied to clipboard

Having an id on a path causes red screen on re-render

Open gpEscrivo opened this issue 10 years ago • 1 comments

If I have an id on a path, on re-render I get:

Cannot read property 'refs' of null

On line 51 of path.js

  if (props.id) {
      return <Defs.Item
          id={props.id}
          svgId={props.svgId}
          visible={true}
      >
          <Path
              ref={ele => this.root = ele.refs.root}  //<------------ here
              {...props}
              id={null}
          />
      </Defs.Item>;
  }

From the react documentation:

Note that when the referenced component is unmounted and whenever the ref changes, the old ref will be called with null as an argument. This prevents memory leaks in the case that the instance is stored, as in the second example. Also note that when writing refs with inline function expressions as in the examples here, React sees a different function object each time so on every update, ref will be called with null immediately before it's called with the component instance.

This fixes the error:

  if (props.id) {
      return <Defs.Item
          id={props.id}
          svgId={props.svgId}
          visible={true}
      >
          <Path
              ref={ele => this.root = (ele !== null) ? ele.refs.root : null }  //<-------- change to this
              {...props}
              id={null}
          />
      </Defs.Item>;
  }

But I haven't tested it much beyond rerendering the offending path. The path didn't actually need the id, it was just left over from conversion of an existing svg, so it should be tested with a path that actually uses it.

gpEscrivo avatar Jul 27 '16 10:07 gpEscrivo

Please update to 4.0.0-rc This has been fixed in the latest version.

magicismight avatar Jul 27 '16 10:07 magicismight