Invariant Violation: Invariant Violation: requireNativeComponent: "RNSVGLinearGradient" was not foun

在React Native项目中引入react-native-svg后出现了错误Invariant Violation: Invariant Violation: requireNativeComponent: "RNSVGLinearGradient" was not foun按照网上说的react-native unlink react-native-svg后重新link都试过不行。最终发现问题如下。

错误信息

Invariant Violation: Invariant Violation: requireNativeComponent: "RNSVGLinearGradient" was not found in the UIManager.

This error is located at:
    in RNSVGLinearGradient (at LinearGradient.js:20)
    in LinearGradient (at PickerView.js:299)
    in RNSVGGroup (at G.js:23)
    in G (at Svg.js:127)
    in RNSVGSvgView (at Svg.js:116)
    in Svg (at PickerView.js:288)
    in RCTView (at View.js:45)
    in View (at PickerView.js:268)
    in RCTView (at View.js:45)
    in View (at PickerView.js:264)

问题分析

如果急着解决问题直接看解决方法吧,可以跳过分析这部分

问题肯定出在link这一步毫无疑问,因为其他的步骤都没有问题。但是当我打开settings.gradle后发现include引入是没有问题的。
然后再看看app/build.gradle也有implementation project(':react-native-svg')[注意如果你是旧版的sdk则不是implementation,没关系的]。但是打开app/java/[包名]/MainApplication.java后发现了问题。因为在getPackages()中并没有new SvgPackage(),所以添加就可以了。

解决办法

在此操作之前请确保你已经执行过react-native link react-native-svg

打开 MainApplication.java

import com.horcrux.svg.SvgPackage;    <--新加行

@Override
    protected List getPackages() {
      return Arrays.asList(
        new SvgPackage()     <--新加行【如果本身有就找其他原因】
       );
    }

你可能感兴趣的:(前端,react,native)