Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we make an animated strike through on react native text?

I have a todo list, each list item has a check box. When I check the box, an animated strike-through effect should show from left to right. How to do that?

like image 668
Supriya Gorai Avatar asked Dec 12 '20 06:12

Supriya Gorai


People also ask

How do you add strikethrough in text React Native?

Use the textDecoration property to strikethrough text in React, e.g. <span style={{textDecoration: 'line-through'}}> .

How do you strike out text in React?

To cross out text on click in React:Set the onClick prop on the element. When the element is clicked, check if its text-decoration property is set. If the property is set, remove it, otherwise set it to line-through .

How to create strikethrough text in react native app using CSS stylesheet?

Lets see the below steps that helps to create Strikethrough text in react native app using css stylesheet. Step 1: Create a new react native project, if you don’t know how to create a new project in react native just follow this tutorial. Step 2: Open App.js File in your favorite code editor and erase all code and follow this tutorial.

How to create an app in React Native?

Step 1: Create a new react native project, if you don’t know how to create a new project in react native just follow this tutorial. Step 2: Open App.js File in your favorite code editor and erase all code and follow this tutorial. Step 3: Through react , react-native packages import all required components.

Can I use animated text input in React Native?

Fully customizable, animated text input for React Native with beautiful and elegant design. Zero Dependency! There is advanced usage on example. should work of the example project. TextInput and Icon is fully customizable thanks to prop-drilling, you can use any TextInput props.

Which React components can be animated?

Only animatable components can be animated. These unique components do the magic of binding the animated values to the properties, and do targeted native updates to avoid the cost of the React render and reconciliation process on every frame. They also handle cleanup on unmount so they are safe by default.


1 Answers

Unfortunately, you can't animate strike directly but I used some tricks to achieve the same effect.

import React from 'react';
import { Animated, Easing, Text, View, StyleSheet } from 'react-native';

export default function App() {
  const ref = React.useRef(View.prototype);
  const animatedValue = React.useRef(new Animated.Value(0)).current;

  const [textWidth, setTextWidth] = React.useState(0);
  const [textHeight, setTextHeight] = React.useState(0);

  React.useEffect(() => {
    ref.current.measure((x, y, w, h) => {
      setTextWidth(w);
      setTextHeight(h);
      animateStrike();
    });
  }, []);

  const animateStrike = () => {
    Animated.timing(animatedValue, {
      toValue: 1,
      duration: 2000,
      easing: Easing.linear,
      useNativeDriver: false,
    }).start();
  };

  const strikeWidth = animatedValue.interpolate({
    inputRange: [0, 1],
    outputRange: [0, textWidth],
    extrapolate: 'clamp',
  });

  return (
    <View style={styles.container}>
      <View>
        <Text style={styles.text} ref={ref}>
          Some Dummy Text
        </Text>
        <Animated.View
          style={[
            styles.strike,
            { width: strikeWidth, top: textHeight / 2 + 1 },
          ]}
        />
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  text: {
    fontSize: 20,
    fontWeight: '400',
  },
  strike: {
    position: 'absolute',
    height: 2,
    backgroundColor: 'red',
  },
});

You can check the demo here.

like image 81
Leri Gogsadze Avatar answered Nov 15 '22 22:11

Leri Gogsadze