本机反应:"only the original thread that created a view hierarchy can touch its views"

React native: "only the original thread that created a view hierarchy can touch its views"

按下按钮获取 svg 的 base64 字符串...产生以下错误: - 只有创建视图层次结构的原始线程才能触及它的视图

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

有什么帮助吗?

我终于想通了...

这是 npm (v 6.4.1) 问题,因为当我删除 node-modules 文件夹和 package-lock.json 并尝试使用 yarn 重新安装它时,它像以前一样工作。

我安装了一个较新版本的 react-native-svg,它对我不起作用,当我尝试使用 npm 降级到旧版本时,出于某种原因它无法识别它,并且 package-lock.json 使用我卸载的较新版本继续创建!

在我的例子中,我通过 运行 我在主线程上的块来解决它:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // My block, doing stuff on the view
    }
});