React-native-reanimated: How can I use it inside a class component?

Created on 5 Nov 2020  路  2Comments  路  Source: software-mansion/react-native-reanimated

Hi,

Thanks for the library.

This code is working perfectly:

import React, {Component} from 'react';
import {View, Button} from 'react-native';
import Animated, {
  useSharedValue,
  useAnimatedStyle,
  withSpring,
} from 'react-native-reanimated';

export default function Box() {
  const offset = useSharedValue(0);

  const animatedStyles = useAnimatedStyle(() => {
    return {
      transform: [{translateX: withSpring(offset.value * 255)}],
    };
  });

  return (
    <View>
      <Animated.View
        style={[
          {height: 100, width: 100, backgroundColor: 'green'},
          animatedStyles,
        ]}
      />
      <Button onPress={() => (offset.value = Math.random())} title="Move" />
    </View>
  );
}

But I want to use it inside a class component.

Like this,

export default class Index extends Component {
 render() {
  return (<View>{this.myFunction() or myFunction...}</View>);
 }
}

Please help.
Thanks in advance.

鉂換uestion 馃彔 Reanimated2

Most helpful comment

You need to import the Box function and use it like that

export default class Index extends Component {
    render() {
        return (
            <View>
                <Box />
            </View>
        )
    }
}

All 2 comments

You need to import the Box function and use it like that

export default class Index extends Component {
    render() {
        return (
            <View>
                <Box />
            </View>
        )
    }
}

@Eduardo-Balistieri it works! Thank you so much.

Was this page helpful?
0 / 5 - 0 ratings