Skip to content Skip to sidebar Skip to footer

React Animation Animated Component Undefined

I want to create a circular animation like a wave and i found that code on the internet. And i converted and changed it with react hooks but then this code does not work correctly.

Solution 1:

There are a couple of problems I've found.

The first is that the class based example you linked maps over the stateAnimations array, but you map over [stateAnimations]. So since stateAnimations is already an array you don't need to surround it with another array. If you fix this the error you posted in your question goes away.

I also found that the working class-based example uses version 8.6.2 of popmotion and the code in the example apparently doesn't work anymore for version 9+.

So after downgrading to 8.6.2 the following code worked for me:

importReact, { useState, useEffect } from"react";
import { Animated, Text, View, StyleSheet } from"react-native";
import { keyframes, stagger, tween } from"popmotion";
import { FontAwesome } from"@expo/vector-icons";

constCOUNT = 4;
constDURATION = 5000;
const initialPhase = { scale: 0, opacity: 1 };
const constructAnimations = [...Array(COUNT).keys()].map(() => initialPhase);

exportdefaultfunctionSearchAnimation() {
  const [stateAnimations, setStateAnimations] = useState(constructAnimations);

  useEffect(() => {
    const actions = Array(COUNT).fill(
      keyframes({
        values: [initialPhase, { scale: 2, opacity: 0 }],
        duration: DURATION,
        loop: Infinity,
        yoyo: Infinity,
      })
    );

    stagger(actions, DURATION / COUNT).start((animations) => {
      setStateAnimations(animations);
    });
  }, []);

  return (
    <Viewstyle={styles.container}>
      {stateAnimations.map(({ opacity, scale }, index) => {
        return (
          <Animated.Viewkey={index}style={[styles.circle,
              {
                transform: [{ scale }],
                opacity,
              },
            ]}
          />
        );
      })}
      <Viewstyle={styles.midCircle}><FontAwesomename="phone"style={styles.icon} /><Textstyle={styles.text}>Searching</Text></View></View>
  );
}

constgetCircle = (radius, backgroundColor = "gold") => ({
  backgroundColor,
  width: radius * 2,
  height: radius * 2,
  borderRadius: radius,
  position: "absolute",
});

const styles = StyleSheet.create({
  icon: {
    color: "white",
    fontSize: 42,
    marginBottom: 5,
  },
  text: {
    color: "white",
    fontSize: 18,
  },
  circle: getCircle(100),
  midCircle: {
    ...getCircle(75),
    alignItems: "center",
    justifyContent: "center",
  },
  container: {
    flex: 1,
    alignItems: "center",
    backgroundColor: "#fff",
    justifyContent: "center",
  },
});

Post a Comment for "React Animation Animated Component Undefined"