Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
imanderson opened this issue Feb 26, 2024 · 10 comments · Fixed by #2405
Closed

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

imanderson opened this issue Feb 26, 2024 · 10 comments · Fixed by #2405

Comments

@imanderson
Copy link

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 facebook/react-native#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.

@buschco
Copy link

buschco commented Apr 19, 2024

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

@imanderson
Copy link
Author

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

No problem! Im glad it helps 😊

@bohdanprog
Copy link
Member

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 bohdanprog added the Close when stale This issue is going to be closed when there is no activity for a while label Aug 8, 2024
@buschco
Copy link

buschco commented Aug 8, 2024

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

@bohdanprog
Copy link
Member

bohdanprog commented Aug 8, 2024

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

@github-actions github-actions bot removed the Close when stale This issue is going to be closed when there is no activity for a while label Aug 8, 2024
@buschco
Copy link

buschco commented Aug 8, 2024

@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>
  );
}

@bohdanprog
Copy link
Member

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

@bohdanprog
Copy link
Member

@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.

@buschco
Copy link

buschco commented Aug 9, 2024

@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);
           });
Screen.Recording.2024-08-09.at.09.24.48.mov

@bohdanprog
Copy link
Member

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.

jakex7 pushed a commit that referenced this issue Aug 13, 2024
)

# Summary
Closes #2233 
in iOS and old Arch, a callback on `toDataURL` method will only be
called the second time of asking.
Based on that
[PR](#2234)
format `RNSVGSvgViewModule` file.

## Test Plan
We can easily test that fix by running the `Test2233`, we have an
example of the problem.

### What are the steps to reproduce (after prerequisites)?

## Compatibility

| OS      | Implemented |
| ------- | :---------: |
| iOS     |    ✅      |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants