This article is about fixing Error Element type is invalid expected a string (for built-in components) or a classfunction (for composite components) but got object in kristerkari react-native-svg-transformer
  • 01-Feb-2023
Lightrun Team
Author Lightrun Team
Share
This article is about fixing Error Element type is invalid expected a string (for built-in components) or a classfunction (for composite components) but got object in kristerkari react-native-svg-transformer

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object in kristerkari react-native-svg-transformer

Lightrun Team
Lightrun Team
01-Feb-2023

Explanation of the problem

Problem Description: A developer is encountering an error in their React Native application built with Expo. The error message states that the “Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.”

Cause: The error is caused by the React Native application trying to render an object as a component, which is not a valid React component. In React, components must be either built-in or defined as a class or function.

Solution: To resolve this error, the developer should ensure that the object being rendered is a valid React component, either built-in or defined as a class or function. This can be accomplished by using the correct import statement for the component, or by properly defining the component as a class or function.

Here is an example of a correctly defined React component:

import React, { Component } from 'react';

class MyComponent extends Component {
  render() {
    return <div>Hello World!</div>;
  }
}

export default MyComponent;

In addition, the developer should also ensure that the correct versions of the dependencies are being used in the package.json file.

"expo": "~40.0.0",
"react": "16.13.1",
 "react-native-svg": "12.1.0",
 "react-native-svg-transformer": "^0.14.3"

The developer should also verify that the Metro configuration file is correctly set up to handle the .svg file type.

const { getDefaultConfig } = require('@expo/metro-config');

module.exports = (async () => {
  const {
    resolver: { sourceExts, assetExts }
  } = await getDefaultConfig(__dirname);
  return {
    transformer: {
      babelTransformerPath: require.resolve('react-native-svg-transformer')
    },
    resolver: {
      assetExts: assetExts.filter((ext) => ext !== 'svg'),
      sourceExts: [...sourceExts, 'svg']
    }
  };
})();

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object in kristerkari react-native-svg-transformer

The problem described in the text above is related to an issue with the React Native framework, where a component is not being rendered correctly and an error message is displayed indicating that the “Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.”

One possible solution to this issue is to clear the cache and reset the terminal. This can be done by killing the terminal, then using the command react-native start --reset-cache to reset the cache and watchman watch-del-all && rm -rf /tmp/metro-bundler-cache-* && rm -rf /tmp/haste-map-react-native-packager-* to delete the contents of the metro bundler cache. After running these commands, the developer should re-run the project to see if the issue has been resolved.

Another solution to this issue is to revert to an older version of a dependency, as suggested in the text. In this case, the suggestion is to revert to version 0.20.0 of the dependency. Reverting to an older version may resolve the issue as older versions of dependencies can be more stable and compatible with the current setup. It’s important to keep in mind that reverting to an older version may also introduce other issues, so it’s always a good idea to thoroughly test the solution before implementing it in a production environment.

Other popular problems with react-native-svg-transformer

Problem: Inconsistent Rendering

A common issue with the react-native-svg-transformer is inconsistent rendering of SVGs. This issue can arise when the transformer fails to accurately interpret the SVG code, resulting in an incorrect or broken rendering of the image.

Solution:

One solution to this issue is to manually update the metro.config.js file, which contains the configuration for the metro bundler. The following code block demonstrates how to configure the react-native-svg-transformer in the metro.config.js file to ensure accurate rendering of SVGs:

const { getDefaultConfig } = require('@expo/metro-config');

module.exports = (async () => {
  const {
    resolver: { sourceExts, assetExts }
  } = await getDefaultConfig(__dirname);
  return {
    transformer: {
      babelTransformerPath: require.resolve('react-native-svg-transformer')
    },
    resolver: {
      assetExts: assetExts.filter((ext) => ext !== 'svg'),
      sourceExts: [...sourceExts, 'svg']
    }
  };
})();

Problem: Compatibility Issues with React Native Versions

Another issue with the react-native-svg-transformer is compatibility with different versions of React Native. If a developer is using an outdated version of react-native-svg-transformer, they may encounter errors or compatibility issues with their React Native setup.

Solution:

To resolve compatibility issues with React Native, the developer should ensure that they are using the latest version of the react-native-svg-transformer that is compatible with their version of React Native. This can be done by checking the compatibility information provided in the react-native-svg-transformer documentation, and updating the version of the transformer in their package.json file as needed.

"dependencies": {
  "expo": "~40.0.0",
  "react": "16.13.1",
  "react-native-svg": "12.1.0",
  "react-native-svg-transformer": "^0.14.3"
}

Problem: Performance Issues

The react-native-svg-transformer can also cause performance issues, particularly when dealing with large or complex SVGs. This can result in slow load times or poor performance within the application.

Solution:

To resolve performance issues with the react-native-svg-transformer, the developer should consider using performance optimization techniques such as minifying the SVG code, or reducing the complexity of the image. Additionally, the developer may want to consider using alternative solutions for rendering SVGs, such as using raster images instead of vector images.

A brief introduction to react-native-svg-transformer

React-Native-SVG-Transformer is a library that is used for transforming SVGs into React Native components. The library acts as a babel transformer and is specifically designed to be used with Expo and React Native. With the use of this library, developers can easily display SVGs in their React Native applications with the ability to apply different styles and animations to them.

React-Native-SVG-Transformer uses Babel to convert SVGs into React Native components at compile time. This helps to improve the performance of applications that use SVGs as the SVGs are pre-processed and converted into optimized components. This also ensures that the SVGs are compatible with the React Native platform and are easily customizable, thus providing a more seamless experience for developers. The library supports all the features that are available in React Native, and it also provides additional features such as the ability to display SVGs as icons, images or other elements within the application.

Most popular use cases for react-native-svg-transformer

  1. Displaying and customizing SVGs in React Native applications: React-Native-SVG-Transformer allows developers to display SVGs in their React Native applications and apply different styles and animations to them. This provides a flexible and powerful solution for including SVGs within React Native applications.
  2. Optimizing performance of applications using SVGs: By using the React-Native-SVG-Transformer library, developers can ensure that their applications perform optimally even when using SVGs. This is because the library pre-processes SVGs and converts them into optimized React Native components at compile time, thus reducing the load on the application’s runtime.
import { Svg, Path } from 'react-native-svg';

const AppIcon = ({ width, height, fill }) => (
  <Svg width={width} height={height} viewBox="0 0 24 24">
    <Path
      fill={fill}
      d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"
    />
  </Svg>
);
  1. Ensuring compatibility of SVGs with React Native: React-Native-SVG-Transformer is specifically designed to be used with React Native and Expo, thus ensuring that SVGs are fully compatible with the React Native platform. This helps to provide a more seamless experience for developers, who can use the library to create and display SVGs in their applications without having to worry about compatibility issues.
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By clicking Submit I agree to Lightrun’s Terms of Use.
Processing will be done in accordance to Lightrun’s Privacy Policy.