Open In App

React Spring Imperative updates

Last Updated : 14 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to use Imperative updates using React Spring. React spring is an animation library that makes animating UI elements simple. It is based on spring physics which helps it to achieve a natural look and feel. It is different from other animation libraries where someone has to deal with curves, easing, and time durations, all of which are in sync with each other.

Platforms: React spring is a cross-platform library, it supports react, react-native, web, and many more platforms. It also has support for all browsers.

Imperative Updates: Imperative updates can use to set the default prop to true.

 

Syntax:

useSpring({
    from: { x: ... },
    to: async animate => {
        // The `config` prop below is inherited by
        // both objects in the `to` array.
        await animate({
            to: [{ x: ... }, { x: ... }],
            config: { tension: ... },
        })
    },
})

Steps to create an app in react.

Step 1: Create a new application using the following command.

npx create-react-app reactspringdemo

Step 2: Now move the created project folder using the following command.

cd reactspringdemo

Step 3: Install the react spring library.

npm install react-spring

Project structure: Import the app folder into the application and after that, the structure will look like this.

 

Example1: In the below code, we will make use of the above syntax to demonstrate the use of the Imperative updates.

GFG.jsx

Javascript




import React from 'react';
import { useSpring, animated } from 'react-spring'
  
function OnRest() {
    const styles = useSpring({
        from: { x: 0 },
        to: async animate => {
            // The `config` prop below is inherited by
            // both objects in the `to` array.
            await animate({
                to: [{ x: 100 }, { x: 0 }],
                config: { tension: 100 },
            })
        },
    })
  
    return (
        <animated.div
            style={{
                width: 80,
                margin: 221,
                height: 80,
                backgroundColor: 'green',
                borderRadius: 16,
                ...styles,
            }}
        />
    )
}
export default OnRest;


App.js

Javascript




import React from 'react'
import GFG from './GFG'
  
function App() {
    console.log('hello')
    return (
        <>
            <GFG />
        </>
    );
}
  
export default App;


Step to run the Application: Open the terminal and type the following command.

npm start

Output:

 

Example2:  In the below code, we will make use of the above syntax to demonstrate the use of the Imperative updates. In this example, we have removed the tension.

GFG.jsx

Javascript




import React from 'react';
import { useSpring, animated } from 'react-spring'
  
function GfG() {
    const styles = useSpring({
        from: { x: 0 },
        to: async animate => {
            // The `config` prop below is inherited by
            // both objects in the `to` array.
            await animate({
                to: [{ x: 0 }, { y: 500 }],
  
            })
        },
    })
  
    return (
        <animated.div
            style={{
                width: 80,
                height: 80,
                backgroundColor: 'green',
                borderRadius: 50,
                boxShadow: 'rgb(0,0,0,0.44) 0px 5px 5px',
                display: 'flex',
                alignItems: 'center',
                justifyContent: 'center',
                color: 'green',
                margin: 250,
                ...styles,
            }}
        />
    )
}
export default GfG;


App.js

Javascript




import React from 'react'
import GFG from './GFG'
  
function App() {
    console.log('hello')
    return (
        <>
            <GFG />
        </>
    );
}
  
export default App;


Step to run the application: Open the terminal and type the following command.

npm start

Output:

 

Reference: https://react-spring.dev/common/props#imperative-updates



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads