react-nativereact-spring

react-spring/native rotate animation


I am using react-spring lib. in the react-native application. I have archived most of animation but I can not figure out how to use rotate init.

Here is my current code =>

  <Spring
        from={ { width: '100%', resizeMode: "contain", rotate: 0 } }
        to={ { rotate: 360 } }
        loop
        config={ { duration: 1500 } }
    >
        {({ rotate, ...props }) => (
            <AnimatedImage source={img} style={ {...props, transform: [{ rotate: `${rotate}deg` }]} } />
        )}
    </Spring>

Then get a error Like this =>

TypeError: undefined is not a function (near '...transform.forEach...')

If someone can explain how to achieve the rotate animation with react-spring it would be really helpful.

you can find a simple example here


Solution

  • You can achieve the rotation animation in @react-spring/native by using the interpolations methods.

    const AnimatedImage = animated(Image)
    
    const App = () => {
      
      const { rotate } = useSpring({
            from: { rotate: 0 },
            to: { rotate: 1 },
            loop: { reverse: true },
            config: { duration: 1500 }
        })
      
      return (
        <AnimatedImage source={img} style={{ transform: [ { rotate: rotate.to([0, 1], ['0deg', '360deg']) } ] }} />
      );
    };
    

    It is strange that we can not apply rotation directly, but i haven't found any other method other than this one in @react-spring/native