[Jest] Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function
  • 11-Jun-2023
Lightrun Team
Author Lightrun Team
Share
[Jest] Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function

[Jest] Warning: React.createElement: type is invalid — expected a string (for built-in components) or a class/function

Lightrun Team
Lightrun Team
11-Jun-2023

Explanation of the problem

When running tests for the application, an error is encountered with the following message: “Warning: React.createElement: type is invalid — expected a string (for built-in components) or a class/function (for composite components) but got: undefined.” This error suggests that there might be an issue with exporting or importing components correctly, or a possible mismatch between default and named imports. Specifically, the error occurs when declaring the <SharedElement> component:

 

<SharedElement id={`${id}${company?.logoUrl}`}>

 

In this case, the application utilizes Jest as the testing framework and RNTL (React Native Testing Library) for testing React Native components.

The relevant dependencies and devDependencies for the project are as follows:

 

"dependencies": {
  "@apollo/client": "^3.3.18",
  "react": "17.0.1",
  "react-native": "0.64.1",
  "react-native-shared-element": "^0.7.0",
  "react-navigation-shared-element": "^5.0.0-alpha1"
},
"devDependencies": {
  "@testing-library/react-hooks": "^7.0.0",
  "@testing-library/react-native": "^7.2.0",
  "babel-jest": "^26.6.3",
  "jest": "^26.6.3",
  "react-test-renderer": "17.0.1"
}

 

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: [Jest] Warning: React.createElement: type is invalid — expected a string (for built-in components) or a class/function

To resolve the error related to the <SharedElement> component in the testing environment, you can try the following steps:

  1. Verify Component Export: Ensure that the component is properly exported from the file where it is defined. Check if the SharedElement component is exported as the default export or as a named export. For example, if it is a default export, the export statement should look like this:

 

export default SharedElement;

 

  1. Check Import Statements: Double-check the import statements in the test file or any other relevant files where the component is used. Ensure that the import statement for the SharedElement component is correct and matches the export statement in the component file. For example, if it is a default export, the import statement should be:

 

import SharedElement from 'react-native-shared-element';

 

  1. Validate Dependencies: Verify that the required dependencies for the react-native-shared-element package and other related packages are correctly installed and up-to-date. You can use the package manager (such as npm or yarn) to check for any updates or inconsistencies in the installed dependencies. Additionally, ensure that the versions of the dependencies listed in the package.json file align with the ones specified.
  2. Review Configuration: Check if there are any specific configuration settings or setup required for the react-native-shared-element library in the testing environment. Refer to the library’s documentation or community resources to ensure that the necessary configurations are in place.

By following these steps and addressing any issues found during the verification process, you should be able to resolve the error related to the SharedElement component and successfully run the tests without encountering the React element type error.

 

Other popular problems with react-navigation-shared-element

Problem 1: Incompatibility with React Navigation Versions One common problem with react-navigation-shared-element is compatibility issues with different versions of React Navigation. This can lead to errors or unexpected behavior when using shared element transitions. For example, if you are using an incompatible version of react-navigation-shared-element with the React Navigation library, you may encounter errors such as “undefined is not an object” or unexpected animation glitches.

Solution: To resolve this issue, ensure that you are using compatible versions of react-navigation-shared-element and React Navigation. It is recommended to refer to the compatibility matrix or documentation provided by the library to determine the compatible versions. Update both libraries to their latest versions or choose versions that are known to work well together. Here is an example of how to install compatible versions using npm:

 

npm install react-navigation@5.x.x react-navigation-shared-element@^5.0.0-alpha1

 

Problem 2: Missing or Incorrect Configuration Another problem that can arise is when the configuration for react-navigation-shared-element is missing or set up incorrectly. This can result in shared element transitions not working as expected or not being applied at all. Common configuration issues include missing wrapper components, incorrect usage of the library’s APIs, or misconfigured transition props.

Solution: Review the documentation and examples provided by react-navigation-shared-element to ensure that you have properly configured the library. Pay attention to the required wrapper components, such as SharedElement and SharedElementRenderer, and their correct placement within your navigation stack. Verify that you are using the correct transition props and passing the necessary data to enable shared element transitions. Here is an example of how to configure shared element transitions with React Navigation 5:

 

import { createSharedElementStackNavigator } from 'react-navigation-shared-element';

const Stack = createSharedElementStackNavigator();

function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName="Home" headerMode="none">
        <Stack.Screen name="Home" component={HomeScreen} />
        <Stack.Screen
          name="Detail"
          component={DetailScreen}
          sharedElementsConfig={(route, otherRoute, showing) => {
            const { item } = route.params;
            return [{ id: `item.${item.id}.photo` }];
          }}
        />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

 

Problem 3: Performance Issues with Large Data Sets When dealing with a large number of shared elements or complex animations, react-navigation-shared-element may exhibit performance issues. This can manifest as laggy transitions or delays in rendering the shared elements, especially on lower-end devices or with a high number of elements to animate.

Solution: To optimize performance, consider the following strategies:

  1. Reduce the number of shared elements: Limit the number of shared elements on the screen to only what is necessary for a smooth user experience.
  2. Optimize animations: Optimize your animations and transitions to minimize unnecessary calculations and improve performance. Use techniques such as hardware acceleration, limiting the number of running animations, or optimizing the rendering pipeline.
  3. Test on different devices: Test your application on a range of devices, including lower-end devices, to identify performance bottlenecks and fine-tune your implementation accordingly.

By addressing these performance considerations and optimizing your implementation, you can improve the performance of shared element transitions in your React Navigation application.

 

A brief introduction to react-navigation-shared-element

React Navigation Shared Element is a powerful library that provides support for shared element transitions in React Native applications built with React Navigation. It allows for smooth and visually appealing transitions between screens by animating shared elements, such as images or text, between different routes. This library is particularly useful when creating immersive user interfaces or showcasing visual content where smooth transitions play a crucial role.

At its core, react-navigation-shared-element leverages the concept of shared elements, where an element is smoothly transitioned from one screen to another, maintaining its visual continuity. This library integrates seamlessly with React Navigation and provides a simple and intuitive API for configuring shared element transitions. Developers can define shared elements by specifying unique identifiers and configure their transition behavior, including animation duration, easing, and interpolation.

To use react-navigation-shared-element, developers need to set up a compatible version of React Navigation and include the necessary wrapper components in their navigation stack. By correctly configuring shared element transitions and providing appropriate shared element identifiers, developers can achieve visually pleasing and seamless transitions that enhance the user experience. This library empowers developers to create engaging interfaces and bring their apps to life with captivating shared element animations.

Most popular use cases for react-navigation-shared-element

  1. Enhanced User Experience with Shared Element Transitions: React Navigation Shared Element allows developers to create visually appealing and seamless transitions between screens in React Native applications. By animating shared elements, such as images, text, or UI components, users can experience smooth and immersive navigation. With just a few lines of code, developers can configure shared element transitions to enhance the overall user experience.

Example:

 

import { createSharedElementStackNavigator } from 'react-navigation-shared-element';

const Stack = createSharedElementStackNavigator();

function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName="Home">
        <Stack.Screen name="Home" component={HomeScreen} />
        <Stack.Screen
          name="Details"
          component={DetailsScreen}
          sharedElementsConfig={(route, otherRoute, showing) => {
            const { id } = route.params;
            return [{ id }];
          }}
        />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

 

  1. Seamless Integration with React Navigation: React Navigation Shared Element seamlessly integrates with React Navigation, the popular navigation library for React Native. It provides wrapper components and hooks that can be used alongside React Navigation’s stack navigator, allowing developers to incorporate shared element transitions into their existing navigation flow. This integration ensures compatibility and ease of use for developers familiar with React Navigation.
  2. Configurable and Customizable Transitions: React Navigation Shared Element offers a range of configuration options to customize the shared element transitions according to specific application requirements. Developers can control various aspects of the transitions, including animation duration, easing functions, and interpolation. This flexibility enables developers to create unique and visually stunning effects that align with the app’s design and branding.

Overall, React Navigation Shared Element is a powerful tool that empowers developers to create engaging and immersive user experiences in React Native applications through shared element transitions.

 

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 submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.