Unlock the Power of Realistic Animations in React

Getting Started with React Motion

React Motion is a popular animation library that makes it easy to create realistic, physics-based animations for your React applications. In this guide, we’ll show you how to install React Motion, explore its core features, and build stunning animations for your React projects.

Installing React Motion

To get started, create a new React project and run the following command in your project root:

npm install react-motion

Understanding React Motion Exports

The react-motion library exports several essential components and helper functions, including:

  • spring: A helper function that defines how a component animates
  • presets: An object of predefined animation properties
  • Motion: A component used to animate a single component
  • StaggeredMotion: A component used to animate multiple components whose animation depends on each other
  • TransitionMotion: A component used to animate the mount and unmount of components

The Spring Helper Function

The spring helper function is a crucial part of React Motion. It takes two arguments: the value and an optional animation config parameter. For example:

spring(10, { stiffness: 130, damping: 42 })

This code animates the value to 10 with a stiffness of 130 and damping of 42. Don’t worry if you’re not familiar with these animation properties – we’ll cover them in more detail later.

The Presets Object

The presets object provides predefined animation configurations. You can use these presets to quickly create different animation effects. For example:

spring(25, presets.wobbly)

This code uses the wobbly preset to animate the value to 25.

The Motion Component

The <Motion /> component is used to animate a single component. It takes two props: defaultStyle and style. The defaultStyle prop defines the initial values of the style object, while the style prop defines the style values at any given point.

Putting it all Together

Let’s create a basic animation using React Motion. First, import the necessary components and helper functions:

import { Motion, spring, presets } from 'eact-motion';

Next, create an <h1> element and wrap it with the <Motion /> component:


<Motion defaultStyle={{ opacity: 0, translateY: 30 }} style={{ opacity: spring(1), translateY: spring(0) }}>
{(interpolatedStyle) => <h1 style={interpolatedStyle}>Hello World!</h1>}
</Motion>

This code creates a simple animation that fades in and translates the <h1> element.

Triggering Animations with a Button

You can also trigger animations dynamically using state. Let’s add a button to our example:

“`
import { useState } from ‘eact’;
import { Motion, spring, presets } from ‘eact-motion’;

function App() {
const [startAnimation, setStartAnimation] = useState(false);

return (

);
}
“`

This code adds two buttons to our example: one to start the animation and another to reset it.

Using React Motion with Styled Components

React Motion can be used with any UI library for React, including styled components. Let’s create a styled <Title /> component:

“`
import styled from ‘tyled-components’;
import { Motion, spring, presets } from ‘eact-motion’;

const Title = styled.h1
font-size: 36px;
color: #333;
;

function App() {
return (
}

);
}
“`

This code creates a styled <Title /> component and uses React Motion to animate it.

Next Steps

React Motion is a powerful animation library that offers a lot more than what we’ve covered in this guide. We recommend exploring the <StaggeredMotion /> and <TransitionMotion /> components, which provide advanced implementation for more complex animations.

Get started with LogRocket’s modern React error tracking in minutes. Visit https://logrocket.com/signup/ to get an app ID and start tracking errors in your React application.

Leave a Reply

Your email address will not be published. Required fields are marked *