0

enter image description herepressing the button to get the base64 string of the svg ... produces the following error: - Only the original thread that created a view hierarchy can touch its views

render() {
    return (
      <ScrollView contentContainerStyle={styles.container}>
        <Svg
          ref={c => (this.svg = c)}
          height="50%"
          width="50%"
          viewBox="0 0 100 100"
        >
          <Circle
            cx="50"
            cy="50"
            r="45"
            stroke="blue"
            strokeWidth="2.5"
            fill="green"
          />
        </Svg>

        <TouchableOpacity
          onPress={() => {
            this.svg.toDataURL(data => {
              console.log("data", data);
            });
          }}
        >
          <Text>Get Data</Text>
        </TouchableOpacity>
      </ScrollView>
    );
  }

Any help?

Chris
  • 826
  • 10
  • 26
Hend El-Sahli
  • 6,268
  • 2
  • 25
  • 42
  • which thread is calling this `render()` ? – Shark Mar 15 '19 at 14:40
  • in a very simple app ... in app.js – Hend El-Sahli Mar 15 '19 at 14:41
  • [mcve] and please post the entire stacktrace so that people can help you. pasting one method and a big screenshot is really of no help to anyone. from the thing you posted so far, i'd guess that you're trying to create views from a non-UI (not main) thread which is a problem in android. – Shark Mar 15 '19 at 17:38

2 Answers2

0

I finally figured this out ...

It was npm (v 6.4.1) issue, cause when I removed node-modules folder and package-lock.json and tried to install it back using yarn, it worked like before.

I installed a newer version of react-native-svg, which does not work for me, when I tried to downgrade to an older version using npm, for some reason it does not recognize it, and package-lock.json kept creating with the newer version i uninstalled!

Hend El-Sahli
  • 6,268
  • 2
  • 25
  • 42
0

In my case, I solved it by literally running my block on the main thread:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // My block, doing stuff on the view
    }
});
ofundefined
  • 2,692
  • 2
  • 18
  • 35