question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Animated in react-native with expo-linear-gradient cause "createAnimatedComponent does not support stateless functional components"

See original GitHub issue

🐛 Bug Report

Summary of Issue

Invariant Violation: createAnimatedComponent does not support stateless functional components; use a class component instead.

Reproducible Demo

import React, { Component } from 'react'
import { LinearGradient } from "expo-linear-gradient";
import { Animated } from "react-native";

class Project extends Component {
  render() {
    return (
       <AnimatedLinearGradient
          colors={["rgba(255,255,255, 0)", "rgba(255,255,255, 1)"]}
          style={{ ... }}
       />
    )
  }
}

export default Project

const AnimatedLinearGradient = Animated.createAnimatedComponent(LinearGradient); // This line will cause an error.

Version

"expo-linear-gradient": "~8.3.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-39.0.2.tar.gz",

Expected Behavior vs Actual Behavior

I want to use Animated with LinearGradient, but it not work.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
brentvatnecommented, Oct 8, 2020

in https://github.com/expo/expo/commit/257e88788838257c4983e001fd3b31c8577f293b#diff-3e93ce91136d0e7afb9dc92f5d0c1fe6 we rewrote it to be a function component. it looks like we need to use React.forwardRef and pass the ref through to the native component

2reactions
cristian-mileacommented, Oct 22, 2020

found a temporary fix using this HOC for this if anyone else is looking

import React, { ComponentType } from "react";
import Animated from "react-native-reanimated";

export function withAnimated(
  WrappedComponent: React.ComponentType<any>,
): ComponentType {
  const displayName =
    WrappedComponent.displayName || WrappedComponent.name || 'Component';

  class WithAnimated extends React.Component {
    static displayName = `WithAnimated(${displayName})`;

    render(): React.ReactNode {
      return <WrappedComponent {...this.props} />;
    }
  }

  return Animated.createAnimatedComponent(WithAnimated);
}

const AnimatedLinearGradient = withAnimated(LinearGradient);

Read more comments on GitHub >

github_iconTop Results From Across the Web

I have a class Component but I get this ...
I have a class Component but I get this `createAnimatedComponent` does not support stateless functional components; use a class component ...
Read more >
LinearGradient - Expo Documentation
LinearGradient. expo-linear-gradient provides a native React view that transitions between multiple colors in a linear direction.
Read more >
Use Functional component as Animated Component - FyndX
In React Native at this moment Animated.createAnimatedComponent can be used only with class based components but not with functional components.
Read more >
Fullstack React Native
One of the major problems that teams face when writing native mobile applications is becoming familiar with all the different technologies.
Read more >
How to Add Linear Gradients as Backgrounds to Your Expo ...
Hi everyone!Today I just have a small tutorial for you on how to use the LinearGradient component from the expo - linear -...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found