DEV Community

Cover image for React Native Text Cycler using reanimated
Nyasha (Nash) Nziramasanga
Nyasha (Nash) Nziramasanga

Posted on

3 3

React Native Text Cycler using reanimated

Below is a code snippet on how to make a text looping effect in React Native with reanimated for animations.

Check out the snack example here

import React, { useEffect, useState } from 'react';
import { StyleProp, TextStyle } from 'react-native';
import Animated, { FadeInDown, FadeOutUp } from 'react-native-reanimated';

const TextCycler = ({
  items,
  textStyles,
  duration = 3000,
  textColors = ['#000000'],
}) => {
  const [index, setIndex] = useState(0);
  const numberOfItems = items.length;

  useEffect(() => {
    const timeout = setInterval(() => {
      setIndex((previousIndex) => {
      // if last item in the array the index is set to 0 meaning start again (looping effect)
        if (previousIndex === numberOfItems - 1) return 0;

        // go to next index
        return previousIndex + 1;
      });
    }, duration);

    // cancels repeating timer
    return () => clearInterval(timeout);
  }, [duration, numberOfItems]);

  return (
    <Animated.Text
      key={index}
      entering={FadeInDown}
      exiting={FadeOutUp}
      style={[textStyles, { color: textColors[index] }]}>
      {items[index]}
    </Animated.Text>
  );
};

export default TextCycler
Enter fullscreen mode Exit fullscreen mode

Sentry mobile image

Mobile Vitals: A first step to Faster Apps

Slow startup times, UI hangs, and frozen frames frustrate users—but they’re also fixable. Mobile Vitals help you measure and understand these performance issues so you can optimize your app’s speed and responsiveness. Learn how to use them to reduce friction and improve user experience.

Read the guide

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs