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

toDataURL only works the second time it is executed in old Arch

Open imanderson opened this issue 1 year ago • 1 comments

Hello,

in iOS and old Arch, I found that the callback on toDataURL method will only be called the second time of asking. This is due to when the queue from UIManager in iOS is flushed. See https://github.com/facebook/react-native/issues/1365

I traced the problem to the iOS implementation and will do a PR with my fix.

Environment info

React native info output:

System:
  OS: macOS 14.3.1
  CPU: (12) arm64 Apple M2 Max
  Memory: 96.39 MB / 32.00 GB
  Shell:
    version: 3.6.0
    path: /opt/homebrew/bin/fish
Binaries:
  Node:
    version: 18.19.1
    path: ~/.nvm/versions/node/v18.19.1/bin/node
  Yarn:
    version: 1.22.21
    path: ~/.nvm/versions/node/v18.19.1/bin/yarn
  npm:
    version: 10.2.4
    path: ~/.nvm/versions/node/v18.19.1/bin/npm
  Watchman:
    version: 2024.01.22.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.14.3
    path: /opt/homebrew/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.2
      - iOS 17.2
      - macOS 14.2
      - tvOS 17.2
      - visionOS 1.0
      - watchOS 10.2
  Android SDK: Not Found
IDEs:
  Android Studio: 2023.1 AI-231.9392.1.2311.11330709
  Xcode:
    version: 15.2/15C500b
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.10
    path: /usr/bin/javac
  Ruby:
    version: 2.6.10
    path: /usr/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.73.4
    wanted: 0.73.4
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false

Library version: 14.1.0

Steps To Reproduce

Simply run the Examples App in this repo with react-native 0.73.4 and go to "Tap the shapes to render the Image below based on the base64-data of the Svg". If you click once, nothing happens, but if you click a second time, the image is rendered.

imanderson avatar Feb 26 '24 10:02 imanderson

Thank you for the issue and the fix. Works well for me 🙏

buschco avatar Apr 19 '24 11:04 buschco

Thank you for the issue and the fix. Works well for me 🙏

No problem! Im glad it helps 😊

imanderson avatar May 21 '24 12:05 imanderson

Hello @imanderson, I tried to reproduce the issue, but couldn't. When I'm using toDataURL everything worked well.

Here is my example:

import { SafeAreaView } from 'react-native';
import Svg, { Path} from 'react-native-svg';

const SvgLogoWelcome = (props) => {
  return (
    <Svg
      ref={(ele) => {
        ele.toDataURL((base64Image) => {
          console.log(base64Image, 'data');
        });
      }}
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 24 24"
      fill="black"
      {...props}>
      <Path d="M12 2c5.514 0 10 4.486 10 10s-4.486 10-10 10S2 17.514 2 12 6.486 2 12 2zm0-2C5.373 0 0 5.373 0 12s5.373 12 12 12 12-5.373 12-12S18.627 0 12 0zm5.507 13.941c-1.512 1.195-3.174 1.931-5.506 1.931-2.334 0-3.996-.736-5.508-1.931L6 14.434C7.127 16.154 9.2 18 12.001 18c2.8 0 4.872-1.846 5.999-3.566l-.493-.493zM8.5 8a1.5 1.5 0 100 3 1.5 1.5 0 000-3zm7 0a1.5 1.5 0 100 3 1.5 1.5 0 000-3z" />
    </Svg>
  );
};

function App() {
  return (
    <SafeAreaView>
      <SvgLogoWelcome />
    </SafeAreaView>
  );
}

bohdanprog avatar Aug 08 '24 12:08 bohdanprog

@bohdanprog just to be sure: are you using the old arch in your example?

buschco avatar Aug 08 '24 13:08 buschco

@buschco Yes, I check the old and new Arch Android and ios platforms.

bohdanprog avatar Aug 08 '24 13:08 bohdanprog

@bohdanprog the problem only occurs, if you want to create the data url from a button press like so:

import * as React from 'react';
import {Button, SafeAreaView, View} from 'react-native';
import Svg, {Path} from 'react-native-svg';

const SvgLogoWelcome = props => {
  const ref = React.useRef();
  return (
    <View>
      <Button
        onPress={() => {
          ref.current?.toDataURL(base64Image => {
            console.log(base64Image, 'data');
          });
        }}
        title="log"
      />
      <Svg
        ref={ref}
        xmlns="http://www.w3.org/2000/svg"
        viewBox="0 0 24 24"
        fill="black"
        {...props}>
        <Path d="M12 2c5.514 0 10 4.486 10 10s-4.486 10-10 10S2 17.514 2 12 6.486 2 12 2zm0-2C5.373 0 0 5.373 0 12s5.373 12 12 12 12-5.373 12-12S18.627 0 12 0zm5.507 13.941c-1.512 1.195-3.174 1.931-5.506 1.931-2.334 0-3.996-.736-5.508-1.931L6 14.434C7.127 16.154 9.2 18 12.001 18c2.8 0 4.872-1.846 5.999-3.566l-.493-.493zM8.5 8a1.5 1.5 0 100 3 1.5 1.5 0 000-3zm7 0a1.5 1.5 0 100 3 1.5 1.5 0 000-3z" />
      </Svg>
    </View>
  );
};

function App() {
  return (
    <SafeAreaView>
      <SvgLogoWelcome />
    </SafeAreaView>
  );
}

buschco avatar Aug 08 '24 19:08 buschco

@buschco, thanks for example. I'm going to check that and let you know ;)

bohdanprog avatar Aug 08 '24 19:08 bohdanprog

@buschco, here is an example, you can test it. https://github.com/bohdanprog/rnsvg-data-url-test

Let me know if it doesn't work because for me everything worked well. Thank you.

bohdanprog avatar Aug 09 '24 06:08 bohdanprog

@bohdanprog same behaviour as described above: the first click wont log the base64, the second one will.

To better demonstrate the issue, I added a console.log('hi') above:

diff --git a/App.js b/App.js
index ab8d424..4adfc19 100644
--- a/App.js
+++ b/App.js
@@ -8,6 +8,7 @@ const SvgLogoWelcome = (props) => {
     <View>
       <Button
         onPress={() => {
+          console.log("hi");
           ref.current?.toDataURL((base64Image) => {
             console.log(base64Image, "data", Platform.OS);
           });

https://github.com/user-attachments/assets/68bc4122-5041-48b4-a643-7c80765bb33b

buschco avatar Aug 09 '24 07:08 buschco

My bad, you're right, sorry, I thought I misclicked on that button that's why I clicked twice. I will check that PR and let you know, again thanks for explaining the main problem.

bohdanprog avatar Aug 11 '24 16:08 bohdanprog