react-native-animateable-text

A fork of React Native's <Text/> component that supports Animated Values as text!

Installation

yarn add react-native-animateable-text
npx pod-install

Usage (Reanimated 2)

Use it the same as a <Text/> component, except instead of passing the text as a child node, pass it using the text props.

import AnimateableText from 'react-native-animateable-text';

const Example: React.FC = () => {
  const text = useSharedValue('World');

  const animatedText = useDerivedValue(() => `Hello ${text.value}`);
  const animatedProps = useAnimatedProps(() => {
    return {
      text: animatedText.value,
    };
  });

  return (
    <AnimateableText
      animatedProps={animatedProps}
      // same other props as Text component
    />;
};

Usage (Reanimated 1)

import AnimateableText from 'react-native-animateable-text';

const Example: React.FC = () => {
  const text = useMemo(() => new Animated.Value('World'), []);

  const animatedText = useMemo(() => concat('Hello', text));

  return (
    <AnimateableText
      text={animatedText}
      // same other props as Text component
    />;
};

OMG, why would you build this?

We want to animate numbers based on gestures as fast as possible, for example for charts displaying financial data. Updating native state is too slow and not feasible for a smooth experience. Using createAnimatedComponent doesn't allow you to animate the text since the children of Text are separate nodes rather than just props.

The best way so far has been to use the <ReText> component from react-native-redash, which allows you to render a string from a Reanimated Text node. However, under the hood, it uses a <TextInput/> and animates it's value prop.

This naturally comes with a few edge cases, for example:

Flicker: When changing values too fast, the text can be cut off and show an ellipsis. The problem gets worse the slower the device and the more congested the render queue is. Watch this GIF at 0.2x speed carefully:

99287990-458d4600-283b-11eb-8d5e-0c1129189c89

Inconsistent styling: When styling a TextInput, you need to add more styles to make it align with the rest of your text. (Behavior in screenshot happens only on Android)

99298147-8c823800-2849-11eb-9939-e326dd8d9f25

Lack of full capabilities: Not all props are available. With Animateable Text, you can use props that you cannot use on a TextInput, such as selectable, dataDetectorType or onTextLayout.

99299532-a15fcb00-284b-11eb-83d2-d3601825a80a

GitHub